PHP中的get_headers()设置超时请求

访问代码

<?php
/**
 * Author: laoge <3385168058@qq.com>
 */
$targetUrl = 'https://www.baidu.com';
// 参数参考
// http://php.net/manual/zh/context.http.php
// https://www.php.net/manual/en/context.http.php
// 设置上下文context默认值
stream_context_set_default(
    array(
        'http' => array(
            'method'  => 'GET',
            'timeout' => (float)0.5, // 发现这个超时时间有点偏大误差,更具实际选择吧
        )
    )
);
$result = get_headers($targetUrl);
echo "<pre>";
print_r($result);
echo "</pre>";

返回结果

/**
 * Author: laoge <3385168058@qq.com>
 */
Array
(
    [0] => HTTP/1.0 200 OK
    [1] => Accept-Ranges: bytes
    [2] => Cache-Control: no-cache
    [3] => Content-Length: 14722
    [4] => Content-Type: text/html
    [5] => Date: Mon, 11 Mar 2019 14:00:42 GMT
    [6] => Etag: "5c7cdb1f-3982"
    [7] => Last-Modified: Mon, 04 Mar 2019 08:00:31 GMT
    [8] => P3p: CP=" OTI DSP COR IVA OUR IND COM "
    [9] => Pragma: no-cache
    [10] => Server: BWS/1.1
    [11] => Set-Cookie: BAIDUID=E934230AC92F534E32050E2EFA12E73A:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
    [12] => Set-Cookie: BIDUPSID=E934230AC92F534E32050E2EFA12E73A; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
    [13] => Set-Cookie: PSTM=1552312842; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
    [14] => Vary: Accept-Encoding
    [15] => X-Ua-Compatible: IE=Edge,chrome=1
)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。