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_versionHTTP协议版本。默认为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" }
以上就是PHP HTTP上下文选项的详细内容,更多请关注其它相关文章!