HTTP context 选锟斤拷

HTTP context 选锟斤拷HTTP context 锟斤拷选锟斤拷锟叫憋拷

说锟斤拷

锟结供锟斤拷 http:// 锟斤拷 https:// 锟斤拷锟斤拷协锟斤拷锟� context 选锟筋。 transports.

锟斤拷选锟斤拷

method string

远锟教凤拷锟斤拷锟斤拷支锟街碉拷 GET锟斤拷POST 锟斤拷锟斤拷锟斤拷 HTTP 锟斤拷锟斤拷锟斤拷

默锟斤拷值锟斤拷 GET锟斤拷

header string

锟斤拷锟斤拷锟节间发锟酵的讹拷锟斤拷 header 锟斤拷锟节达拷选锟斤拷锟街碉拷锟斤拷锟斤拷锟斤拷锟斤拷锟街� 锟斤拷锟斤拷锟斤拷 User-agent:锟斤拷 Host: 锟斤拷 Authentication:锟斤拷锟斤拷

user_agent string

要锟斤拷锟酵碉拷 header User-Agent: 锟斤拷值锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷 header context 选锟斤拷锟斤拷没锟斤拷指锟斤拷 user-agent锟斤拷锟斤拷值锟斤拷锟斤拷使锟矫★拷

默锟斤拷使锟斤拷 php.ini 锟斤拷锟斤拷锟矫碉拷 user_agent锟斤拷

content string

锟斤拷 header 锟斤拷锟斤拷要锟斤拷锟酵的讹拷锟斤拷锟斤拷锟捷★拷通锟斤拷使锟斤拷POST锟斤拷PUT锟斤拷锟斤拷

proxy string

URI 指锟斤拷锟侥达拷锟斤拷锟斤拷锟斤拷锟斤拷牡锟街凤拷锟�(e.g. tcp://proxy.example.com:5100).

request_fulluri boolean

锟斤拷锟斤拷锟斤拷为 TRUE 时锟斤拷锟节癸拷锟斤拷锟斤拷锟斤拷时锟斤拷使锟斤拷锟斤拷锟斤拷 URI 锟斤拷(i.e. GET http://www.example.com/path/to/file.html HTTP/1.0)锟斤拷 锟斤拷然锟斤拷锟斤拷一锟斤拷锟角憋拷准锟斤拷锟斤拷锟斤拷锟绞斤拷锟斤拷锟侥承╋拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷要锟斤拷锟斤拷

默锟斤拷值锟斤拷 FALSE.

follow_location integer

锟斤拷锟斤拷 Location header 锟斤拷锟截讹拷锟斤拷锟斤拷锟斤拷为 0 锟皆斤拷锟矫★拷

默锟斤拷值锟斤拷 1锟斤拷

max_redirects integer

锟斤拷锟斤拷锟截讹拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟街滴� 1 锟斤拷锟斤拷锟斤拷锟斤拷锟轿讹拷锟斤拷锟斤拷锟斤拷囟锟斤拷锟�

默锟斤拷值锟斤拷 20锟斤拷

protocol_version float

HTTP 协锟斤拷姹撅拷锟�

默锟斤拷值锟斤拷 1.0锟斤拷

Note:

PHP 5.3.0 锟斤拷前锟侥版本没锟斤拷实锟街分块传锟斤拷锟斤拷搿� 锟斤拷锟斤拷锟街碉拷锟斤拷锟轿� 1.1 锟斤拷锟斤拷 1.1 锟侥硷拷锟捷斤拷锟斤拷锟斤拷锟斤拷锟斤拷巍锟�

timeout float

锟斤拷取锟斤拷时时锟戒,锟斤拷位为锟诫(s锟斤拷锟斤拷锟斤拷 float 指锟斤拷(e.g. 10.5)锟斤拷

默锟斤拷使锟斤拷 php.ini 锟斤拷锟斤拷锟矫碉拷 default_socket_timeout锟斤拷

ignore_errors boolean

锟斤拷使锟角癸拷锟斤拷状态锟斤拷锟斤拷然锟斤拷取锟斤拷锟捷★拷

默锟斤拷值为 FALSE.

锟斤拷锟斤拷锟斤拷志

锟芥本 说锟斤拷
5.3.4 锟斤拷锟� follow_location锟斤拷
5.3.0 锟斤拷 protocol_version 锟斤拷锟斤拷为 1.1 时支锟街分块传锟斤拷锟斤拷搿�
5.2.10 锟斤拷锟� ignore_errors锟斤拷
5.2.10 header 锟斤拷锟节匡拷锟斤拷锟斤拷一锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷 array锟斤拷
5.2.1 锟斤拷锟� timeout锟斤拷
5.1.0 Added HTTPS proxying through HTTP proxies. 锟斤拷泳锟斤拷锟� HTTP 锟斤拷锟斤拷锟� HTTPS 锟斤拷锟斤拷
5.1.0 锟斤拷锟� max_redirects锟斤拷
5.1.0 锟斤拷锟� protocol_version锟斤拷

锟斤拷锟斤拷

Example #1 锟斤拷取一锟斤拷页锟芥并锟斤拷锟斤拷 POST 锟斤拷锟斤拷

<?php

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

$opts = array('http' =>
    array(
        
'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);

?>

Example #2 锟斤拷锟斤拷锟截讹拷锟津并伙拷取 header 锟斤拷锟斤拷锟斤拷

<?php

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

$opts = array('http' =>
    array(
        
'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);
?>

注锟斤拷

Note: Underlying socket stream context options
Additional context options may be supported by the underlying transport For http:// streams, refer to context options for the tcp:// transport. For https:// streams, refer to context options for the ssl:// transport.

Note: HTTP status line
When this stream wrapper follows a redirect, the wrapper_data returned by stream_get_meta_data() might not necessarily contain the HTTP status line that actually applies to the content data at index 0.

array (
  'wrapper_data' =>
  array (
    0 => 'HTTP/1.0 301 Moved Permantenly',
    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 301 (permanent redirect), so the stream wrapper automatically followed the redirect to get a 200 response (index = 4).