首頁 > php教程 > PHP源码 > 主體

带超时的getHeader函数

WBOY
發布: 2016-06-16 08:39:34
原創
1741 人瀏覽過
跳至 [1] [2] [全屏预览]
    function getHeaders($url,$timeout=2)
    {
        $ch=curl_init();
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_HEADER,true);
        curl_setopt($ch,CURLOPT_NOBODY,true);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
        curl_setopt($ch,CURLOPT_TIMEOUT,$timeout);
        $data=curl_exec($ch);
        curl_close($ch);
        if(empty($data)) return false;
        $headers=explode("\n",$data);
        foreach ($headers as $key=>$headerLine){
            if(strlen($headerLine)>1){
                if(strpos($headerLine,':')!==false) $headers[stristr($headerLine,':',true)]=trim(stristr($headerLine,':'),': ');
            }else{
                unset($headers[$key]);
            }
        }
        return  $headers;
    }
登入後複製

2. [代码]返回值 跳至 [1] [2] [全屏预览]

Array
(
    [0] => HTTP/1.1 200 OK
    [1] => Server: NWS_UGC_HY
    [2] => Connection: keep-alive
    [3] => Date: Tue, 14 Jun 2016 13:27:28 GMT
    [4] => Cache-Control: max-age=600
    [5] => Expires: Tue, 14 Jun 2016 13:37:28 GMT
    [6] => Last-Modified: Sun, 03 Aug 2014 22:52:31 GMT
    [7] => Content-Type: application/octet-stream
    [8] => Content-Length: 102752
    [9] => X-Cache-Lookup: Hit From Disktank
    [Server] => NWS_UGC_HY
    [Connection] => keep-alive
    [Date] => Tue, 14 Jun 2016 13:27:28 GMT
    [Cache-Control] => max-age=600
    [Expires] => Tue, 14 Jun 2016 13:37:28 GMT
    [Last-Modified] => Sun, 03 Aug 2014 22:52:31 GMT
    [Content-Type] => application/octet-stream
    [Content-Length] => 102752
    [X-Cache-Lookup] => Hit From Disktank
)
登入後複製
相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門推薦
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板