PHP HTTP 上下文选项
phpserver side programmingprogramming更新于 2025/7/18 13:37:17
简介
以下是http://和https://传输方式的上下文选项列表
| method | 远程服务器支持的HTTP方法。默认为GET。 |
| header | 请求期间发送的附加标头。 |
| user_agent | 与 User-Agent: 标头一起发送的值。默认情况下,使用 php.ini 中的 user_agent 设置。 |
| content | 标头后发送的附加数据。通常用于 POST 或 PUT 请求。 |
| proxy | 指定代理服务器地址的 URI。 |
| request_fulluri boolean | 设置为 TRUE 时,构建请求时将使用整个 URI。默认为 FALSE。 |
| follow_location | 遵循 Location 标头重定向。设置为 0 表示禁用。默认为 1。 |
| max_redirects | 要遵循的最大重定向次数。 |
| protocol_version | HTTP 协议版本。默认为 1.0。 |
| timeout | 读取超时时间(秒),以浮点数指定(例如 10.5)。 |
| ignore_errors | 即使出现失败状态代码,也获取内容。默认为 FALSE。 |
以下示例从 http:// URL 获取标头和内容
示例
<?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));
?>
输出
这将显示如下标题信息和元数据 −
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"
}

