
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
PHP HTTP context options
Introduction
Given below is the list of context options for http:// and https:// transports
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 boolean | 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. |
Following example fetches headers and contents from http:// URL
Example
<?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)); ?>
Output
This displays header information and metadata as follows −
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" }
- Related Articles
- PHP FTP context options
- PHP MongoDB context options
- PHP Phar context options
- PHP Socket context options
- PHP SSL context options
- PHP Zip context options
- CURL context options in PHP
- PHP http://
- PHP Context Parameters
- Can somebody explain the HTTP headers in simpler terms in JSP context?
- PHP – Detect HTTP input character encoding with mb_http_input()
- PHP – Get or set the HTTP output character encoding with mb_http_output()
- How to add http:// if it doesn't exist in the URL PHP?
- Difference between Android Activity Context and Application Context
- What is HTTP?

Advertisements