PHP HTTP上下文选项

王林
发布: 2023-08-25 20:34:01
转载
1117 人浏览过

PHP HTTP上下文选项

介绍

下面是 http://https://传输的上下文选项列表

method 远程服务器支持的HTTP方法。默认为GET。
header 在请求期间发送的附加头信息。
user_agent 与User-Agent:头信息一起发送的值。默认情况下使用user_agent php.ini设置。
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(&#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));
?>
登录后复制

输出

这将显示标题信息和元数据如下 −

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"
}
登录后复制

以上是PHP HTTP上下文选项的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:tutorialspoint.com
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!