国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

php使用curl獲取header檢測開啟GZip壓縮的方法

2024-05-05 00:05:01
字體:
來源:轉載
供稿:網友

本文實例講述了php使用curl獲取header檢測開啟GZip壓縮的方法。分享給大家供大家參考,具體如下:

獲得網頁header信息,是網站開發人員和維護人員常用的技術。網頁的header信息,非常豐富,非專業人士一般較難讀懂和理解各個項目的含義。

獲取網頁header信息,方法多種多樣,就php語言來說,我作為一個菜鳥,知道的方法也有4種那么多。下面逐一獻上。

方法一:使用get_headers()函數

這個方法很多人使用,也很簡單便捷,只需要兩行代碼即可搞定。如下:

$thisurl = "http://m.survivalescaperooms.com/";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] => vevb.com
    [Date] => Wed, 15 Aug 2018 01:31:48 GMT
    [Connection] => close
    [Content-Length] => 89251
)

方法二:使用http_response_header

代碼也很簡單,僅需三行:

$thisurl = "http://m.survivalescaperooms.com/";$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: vevb.com
    [6] => Date: Wed, 15 Aug 2018 01:34:15 GMT
    [7] => Connection: close
    [8] => Content-Length: 89282
)

方法三:使用stream_get_meta_data()函數

代碼也只有三行:

$thisurl = "http://m.survivalescaperooms.com/";$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: vevb.com
            [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] => http://m.survivalescaperooms.com/
    [timed_out] => 
    [blocked] => 1
    [eof] => 
)

上述三種方法都可以輕松獲得網頁header信息,且包含的信息都已經相當豐富,滿足一般要求,不過比較遺憾的是,上述三種方法都不能用來檢測網頁是否啟用了GZip壓縮。要檢測GZip壓縮,還需其他的方法才行。這里介紹的是用curl()函數來檢測。

使用curl獲得header可以檢測GZip壓縮

先貼出代碼:

<?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

上面輸出結果里可以看到一個項目:Content-Encoding: gzip,這個正是我們用來判斷網頁是否啟用GZip壓縮的項目。

另外,需要認真注意下本實例里的注釋部分,不能少了任何一項,否則可能獲取header信息有誤。


注:相關教程知識閱讀請移步到PHP教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 名山县| 金阳县| 寻乌县| 柞水县| 辉县市| 涞源县| 崇礼县| 修武县| 泰来县| 龙岩市| 定日县| 邯郸市| 平顶山市| 林西县| 常德市| 汝州市| 蓬溪县| 庆城县| 华池县| 繁峙县| 雅江县| 全州县| 阳江市| 云安县| 江都市| 饶河县| 江西省| 扶风县| 长岭县| 彝良县| 临海市| 威远县| 三穗县| 潮州市| 竹溪县| 高平市| 三台县| 永吉县| 阜城县| 永新县| 花垣县|