PHP HTTP context options

PHP HTTP context options allow you to configure HTTP and HTTPS requests when using stream functions like file_get_contents() or fopen(). These options provide control over request methods, headers, timeouts, and other HTTP-specific behaviors.

Available Context Options

Option Description
method HTTP method supported by the remote server. Defaults to GET.
header Additional headers to be sent during request.
user_agent Value to send with User-Agent: header. By default the user_agent php.ini setting is used.
content Additional data to be sent after the headers. Typically used with POST or PUT requests.
proxy URI specifying address of proxy server.
request_fulluri When set to TRUE, the entire URI will be used when constructing the request. Defaults to FALSE.
follow_location Follow Location header redirects. Set to 0 to disable. Defaults to 1.
max_redirects The max number of redirects to follow.
protocol_version HTTP protocol version. Defaults to 1.0.
timeout Read timeout in seconds, specified by a float (e.g. 10.5).
ignore_errors Fetch the content even on failure status codes. Defaults to FALSE.

Example − Basic HTTP Context

The following example demonstrates how to create an HTTP context and retrieve metadata from a URL −

<?php
$url = "http://localhost/testscript.php";
$opts = array('http' =>
    array(
        'method' => 'GET',
        'max_redirects' => '0',
        'ignore_errors' => '1'
    )
);
$context = stream_context_create($opts);
$stream = fopen($url, 'r', false, $context);
var_dump(stream_get_meta_data($stream));
?>
array(10) {
  ["timed_out"]=>
  bool(false)
  ["blocked"]=>
  bool(true)
  ["eof"]=>
  bool(false)
  ["wrapper_data"]=>
  array(7) {
    [0]=>
    string(15) "HTTP/1.1 200 OK"
    [1]=>
    string(35) "Date: Thu, 17 Sep 2020 07:04:47 GMT"
    [2]=>
    string(55) "Server: Apache/2.4.41 (Win64) OpenSSL/1.0.2s PHP/7.1.32"
    [3]=>
    string(24) "X-Powered-By: PHP/7.1.32"
    [4]=>
    string(17) "Content-Length: 0"
    [5]=>
    string(17) "Connection: close"
    [6]=>
    string(38) "Content-Type: text/html; charset=UTF-8"
  }
  ["wrapper_type"]=>
  string(4) "http"
  ["stream_type"]=>
  string(14) "tcp_socket/ssl"
  ["mode"]=>
  string(1) "r"
  ["unread_bytes"]=>
  int(0)
  ["seekable"]=>
  bool(false)
  ["uri"]=>
  string(31) "http://localhost/testscript.php"
}

Example − POST Request with Custom Headers

This example shows how to send a POST request with custom headers and content −

<?php
$data = json_encode(array('name' => 'John', 'email' => 'john@example.com'));

$opts = array('http' =>
    array(
        'method' => 'POST',
        'header' => "Content-Type: application/json\r<br>" .
                   "Authorization: Bearer token123\r<br>",
        'content' => $data,
        'timeout' => 10.0
    )
);

$context = stream_context_create($opts);
$result = file_get_contents('http://api.example.com/users', false, $context);
echo $result;
?>

Conclusion

HTTP context options provide fine-grained control over HTTP requests in PHP streams. They are particularly useful for API integrations, custom authentication, and handling specific HTTP scenarios like redirects or timeouts.

Updated on: 2026-03-15T09:21:17+05:30

607 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements