PHP HTTP context options

PHPServer Side ProgrammingProgramming


Given below is the list of context options for http:// and https:// transports

methodHTTP method supported by the remote server. Defaults to GET.
headerAdditional headers to be sent during request.
user_agentValue to send with User-Agent: header. By default the user_agent php.ini setting is used.
contentAdditional data to be sent after the headers. Typically used with POST or PUT requests.
proxyURI specifying address of proxy server.
request_fulluri booleanWhen set to TRUE, the entire URI will be used when constructing the request. Defaults to FALSE.
follow_locationFollow Location header redirects. Set to 0 to disable.Defaults to 1.
max_redirectsThe max number of redirects to follow.
protocol_versionHTTP protocol version. Defaults to 1.0.
timeoutRead timeout in seconds, specified by a float (e.g. 10.5).
ignore_errorsFetch the content even on failure status codes. Defaults to FALSE.

Following example fetches headers and contents from http:// URL


$url = "http://localhost/testscript.php";
$opts = array('http' =>
   'method' => 'GET',
   'max_redirects' => '0',
   'ignore_errors' => '1'
$context = stream_context_create($opts);
$stream = fopen($url, 'r', false, $context);


This displays header information and metadata as follows −

array(10) {
   array(7) {
      string(15) "HTTP/1.1 200 OK"
      string(35) "Date: Thu, 17 Sep 2020 07:04:47 GMT"
      string(55) "Server: Apache/2.4.41 (Win64) OpenSSL/1.0.2s PHP/7.1.32"
      string(24) "X-Powered-By: PHP/7.1.32"
      string(17) "Content-Length: 0"
      string(17) "Connection: close"
      string(38) "Content-Type: text/html; charset=UTF-8"
string(4) "http"
string(14) "tcp_socket/ssl"
string(1) "r"
string(31) "http://localhost/testscript.php"
Published on 21-Sep-2020 12:49:04