Home  >  Article  >  Backend Development  >  PHP HTTP context options

PHP HTTP context options

王林
王林forward
2023-08-25 20:29:051090browse

PHP HTTP上下文选项

Introduction

The following is a list of context options for http:// and https:// transports

method HTTP method supported by the remote server. Default is GET.
header Additional header information sent during the request.
user_agent The value sent with the User-Agent: header. By default the user_agent php.ini setting is used.
content Additional data sent after the header information. Typically used with POST or PUT requests.
proxy Specifies the URI of the proxy server address.
request_fulluri boolean When set to TRUE, the entire URI will be used when constructing the request. Default is FALSE.
follow_location Redirect that follows the Location header information. Set to 0 to disable. Default is 1.
max_redirects Maximum number of redirects to follow.
protocol_version HTTP protocol version. Default is 1.0.
timeout Read timeout in seconds, specified as a float (e.g. 10.5).
ignore_errors Get content even on failure status code. Default is FALSE.

The following example gets header information and content from http:// URL

Example

<?php
$url = "http://localhost/testscript.php";
$opts = array(&#39;http&#39; =>
array(
   &#39;method&#39; => &#39;GET&#39;,
   &#39;max_redirects&#39; => &#39;0&#39;,
   &#39;ignore_errors&#39; => &#39;1&#39;
);
$context = stream_context_create($opts);
$stream = fopen($url, &#39;r&#39;, false, $context);
var_dump(stream_get_meta_data($stream));
?>

Output

This will display the title 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"
}

The above is the detailed content of PHP HTTP context options. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete