下面给出的是 http://和https://传输的上下文选项列表
| 方法 | 远程服务器支持的HTTP方法。默认为GET。 |
| 标头 | 请求期间将发送其他标头。 |
| 用户代理 | 使用User-Agent发送的值:标头。默认情况下,使用user_agent php.ini设置。 |
| 内容 | 标头后要发送的其他数据。通常与POST或PUT请求一起使用。 |
| 代理 | 指定代理服务器地址的URI。 |
| request_fulluri布尔值 | 如果设置为TRUE,则在构造请求时将使用整个URI。默认为FALSE。 |
| follow_location | 跟随位置标头重定向。设置为0以禁用,默认为1。 |
| max_redirects | 遵循的最大重定向数。 |
| protocol_version | HTTP协议版本。默认为1.0。 |
| 超时 | 读取超时(以秒为单位),由浮点数指定(例如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"
}