PHP.nl

HTTP context options

HTTP context options

HTTP context option listing

Context options for and transports. http://``https://

method``string , , or any other HTTP method supported by the remote server. GET``POST

   Defaults to .
  `GET`

header``array``string Additional headers to be sent during request. Values in this option will override other values (such as , , and ), even when following redirects. Thus it is not recommended to set a header, if is enabled. User-agent:``Host:``Authentication:``Location:``Host:``follow_location

   String value should be  pairs delimited by
   , e.g.
   .
   Array value should be a list of  pairs, e.g.
   .
  `Key: value``\r\n``"Content-Type: application/json\r\nConnection: close"``Key: value``["Content-Type: application/json", "Connection: close"]`

user_agent``string Value to send with header. This value will only be used if user-agent is specified in the context option above. User-Agent:notheader

   By default the
   
   php.ini setting is used.
  user_agent

content``stringAdditional data to be sent after the headers. Typically used with POST or PUT requests.

proxy``string URI specifying address of proxy server (e.g. ). tcp://proxy.example.com:5100

request_fulluri``bool When set to true, the entire URI will be used when constructing the request (e.g. ). While this is a non-standard request format, some proxy servers require it. GET http://www.example.com/path/to/file.html HTTP/1.0

Defaults to false.

follow_location``int Follow header redirects. Set to to disable. Location``0

   Defaults to .
  `1`

max_redirects``int The max number of redirects to follow. Value or less means that no redirects are followed. 1

   Defaults to .
  `20`

protocol_version``floatHTTP protocol version.

   Defaults to  as of PHP 8.0.0;
   prior to that version the default was .
  `1.1``1.0`

timeout``float Read timeout in seconds, specified by a (e.g. ). float``10.5

   By default the
   
   php.ini setting is used.
  default_socket_timeout

ignore_errors``boolFetch the content even on failure status codes.

Defaults to false.

Voorbeeld: Fetch a page and send POST data

<?php

$postdata = http_build_query(
    [
        'var1' => 'some content',
        'var2' => 'doh',
    ]
);

$opts = [
    'http' => [
        'method'  => 'POST',
        'header'  => 'Content-type: application/x-www-form-urlencoded',
        'content' => $postdata,
    ]
];

$context = stream_context_create($opts);

$result = file_get_contents('http://example.com/submit.php', false, $context);

?>

Voorbeeld: Ignore redirects but fetch headers and content

<?php

$url = "http://www.example.org/header.php";

$opts = [
    'http' => [
        'method'        => 'GET',
        'max_redirects' => '0',
        'ignore_errors' => '1',
    ]
];

$context = stream_context_create($opts);
$stream = fopen($url, 'r', false, $context);

// header information as well as meta data
// about the stream
var_dump(stream_get_meta_data($stream));

// actual data at $url
var_dump(stream_get_contents($stream));
fclose($stream);
?>

Opmerking: > ### Underlying socket stream context options

Additional context options may be supported by the

For  streams, refer to context
options for the  transport.  For
 streams, refer to context options
for the  transport.

underlying transporthttp://``tcp://``https://``ssl://

Opmerking: > ### HTTP status line

When this stream wrapper follows a redirect, the
 returned by
 might not necessarily contain
the HTTP status line that actually applies to the content data at index
.

wrapper_data``stream_get_meta_data``0

array (
  'wrapper_data' =>
  array (
    0 => 'HTTP/1.0 301 Moved Permanently',
    1 => 'Cache-Control: no-cache',
    2 => 'Connection: close',
    3 => 'Location: http://example.com/foo.jpg',
    4 => 'HTTP/1.1 200 OK',
    ...
The first request returned a  (permanent redirect),
so the stream wrapper automatically followed the redirect to get a
 response (index = ).

301``200``4