您好,登錄后才能下訂單哦!
這期內容當中小編將會給大家帶來有關如何在php中使用curl獲取header檢測,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
方法一:使用get_headers()
函數
這個方法很多人使用,也很簡單便捷,只需要兩行代碼即可搞定。如下:
$thisurl = "https://www.jb51.net/"; print_r(get_headers($thisurl, 1));
得到的結果為:
Array
(
[0] => HTTP/1.1 200 OK
[Content-Type] => text/html
[Last-Modified] => Wed, 15 Aug 2018 01:23:03 GMT
[ETag] => "99a921833634d41:0"
[Server] => Microsoft-IIS/7.5
[X-Powered-By] => jb51.net
[Date] => Wed, 15 Aug 2018 01:31:48 GMT
[Connection] => close
[Content-Length] => 89251
)
http_response_header
代碼也很簡單,僅需三行:
$thisurl = "https://www.jb51.net/"; $html = file_get_contents($thisurl ); print_r($http_response_header);
得到的結果為:
Array
(
[0] => HTTP/1.1 200 OK
[1] => Content-Type: text/html
[2] => Last-Modified: Wed, 15 Aug 2018 01:33:04 GMT
[3] => ETag: "7b9757e93734d41:0"
[4] => Server: Microsoft-IIS/7.5
[5] => X-Powered-By: jb51.net
[6] => Date: Wed, 15 Aug 2018 01:34:15 GMT
[7] => Connection: close
[8] => Content-Length: 89282
)
stream_get_meta_data()
函數代碼也只有三行:
$thisurl = "https://www.jb51.net/"; $fp = fopen($thisurl, 'r'); print_r(stream_get_meta_data($fp));
得到的結果為:
Array
(
[wrapper_data] => Array
(
[0] => HTTP/1.1 200 OK
[1] => Content-Type: text/html
[2] => Last-Modified: Wed, 15 Aug 2018 01:38:45 GMT
[3] => ETag: "ecc8f8b43834d41:0"
[4] => Server: Microsoft-IIS/7.5
[5] => X-Powered-By: jb51.net
[6] => Date: Wed, 15 Aug 2018 01:39:35 GMT
[7] => Connection: close
[8] => Content-Length: 89421
)
[wrapper_type] => http
[stream_type] => tcp_socket/ssl
[mode] => r
[unread_bytes] => 7945
[seekable] =>
[uri] => https://www.jb51.net/
[timed_out] =>
[blocked] => 1
[eof] =>
)
上述三種方法都可以輕松獲得網頁header信息,且包含的信息都已經相當豐富,滿足一般要求,不過比較遺憾的是,上述三種方法都不能用來檢測網頁是否啟用了GZip壓縮。要檢測GZip壓縮,還需其他的方法才行。這里介紹的是用curl()
函數來檢測。
先貼出代碼:
<?php $szUrl = 'http://www.webkaka.com/'; $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $szUrl); curl_setopt($curl, CURLOPT_HEADER, 1); //輸出header信息 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); //不顯示網頁內容 curl_setopt($curl, CURLOPT_ENCODING, ''); //允許執行gzip $data=curl_exec($curl); if(!curl_errno($curl)) { $info = curl_getinfo($curl); $httpHeaderSize = $info['header_size']; //header字符串體積 $pHeader = substr($data, 0, $httpHeaderSize); //獲得header字符串 $split = array("\r\n", "\n", "\r"); //需要格式化header字符串 $pHeader = str_replace($split, '<br>', $pHeader); //使用<br>換行符格式化輸出到網頁上 echo $pHeader; } ?>
輸出結果如下:
HTTP/1.1 200 OK
Cache-Control: max-age=86400
Content-Length: 15189
Content-Type: text/html
Content-Encoding: gzip
Content-Location: http://www.webkaka.com/index.html
Last-Modified: Fri, 19 Jul 2013 03:52:28 GMT
Accept-Ranges: bytes
ETag: "0268633384ce1:5cb3"
Vary: Accept-Encoding
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
Date: Fri, 19 Jul 2013 09:27:21 GMT
上述就是小編為大家分享的如何在php中使用curl獲取header檢測了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。