PHP HTTP 上下文选项
引言
以下是http://和https://传输的上下文选项列表
| method | 远程服务器支持的 HTTP 方法。默认值为 GET。 |
| header | 在请求期间发送其他标头。 |
| user_agent | 发送 User-Agent: 标头时的值。默认情况下,使用 user_agent php.ini 设置。 |
| content | 在标头之后发送的其他数据。通常与 POST 或 PUT 请求一起使用。 |
| proxy | 指定代理服务器地址的 URI。 |
| request_fulluri 布尔值 | 如果设置为 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 = "https:///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) "https:///testscript.php"
}
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP