技術(shù)頻道導(dǎo)航
HTML/CSS
.NET技術(shù)
IIS技術(shù)
PHP技術(shù)
Js/JQuery
Photoshop
Fireworks
服務(wù)器技術(shù)
操作系統(tǒng)
網(wǎng)站運(yùn)營(yíng)

贊助商

分類目錄

贊助商

最新文章

搜索

php curl獲得header檢測(cè)GZip壓縮的源代碼

作者:admin    時(shí)間:2013-7-19 16:55:17    瀏覽:

獲得網(wǎng)頁(yè)header信息,是網(wǎng)站開(kāi)發(fā)人員和維護(hù)人員常用的技術(shù)。網(wǎng)頁(yè)的header信息,非常豐富,非專業(yè)人士一般較難讀懂和理解各個(gè)項(xiàng)目的含義。

獲取網(wǎng)頁(yè)header信息,方法多種多樣,就php語(yǔ)言來(lái)說(shuō),我作為一個(gè)菜鳥(niǎo),知道的方法也有4種那么多。下面逐一獻(xiàn)上。

方法一:使用get_headers()函數(shù)

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

$thisurl = "http://www.howtostagehomes.com/";
print_r(get_headers($thisurl, 1));

得到的結(jié)果為:

Array
(
    [0] => HTTP/1.1 200 OK
    [Cache-Control] => max-age=86400
    [Content-Length] => 76102
    [Content-Type] => text/html
    [Content-Location] => http://www.howtostagehomes.com/index.html
    [Last-Modified] => Fri, 19 Jul 2013 03:52:30 GMT
    [Accept-Ranges] => bytes
    [ETag] => "50bc48643384ce1:5cb3"
    [Server] => Microsoft-IIS/6.0
    [X-Powered-By] => ASP.NET
    [Date] => Fri, 19 Jul 2013 09:06:39 GMT
    [Connection] => close
)

方法二:使用http_response_header

代碼也很簡(jiǎn)單,僅需三行:

$thisurl = "http://www.howtostagehomes.com/";
$html = file_get_contents($thisurl );
print_r($http_response_header);

得到的結(jié)果為:

Array
(
    [0] => HTTP/1.1 200 OK
    [1] => Cache-Control: max-age=86400
    [2] => Content-Length: 76102
    [3] => Content-Type: text/html
    [4] => Content-Location: http://www.howtostagehomes.com/index.html
    [5] => Last-Modified: Fri, 19 Jul 2013 03:52:30 GMT
    [6] => Accept-Ranges: bytes
    [7] => ETag: "50bc48643384ce1:5cb3"
    [8] => Server: Microsoft-IIS/6.0
    [9] => X-Powered-By: ASP.NET
    [10] => Date: Fri, 19 Jul 2013 09:06:41 GMT
    [11] => Connection: close
)

方法三:使用stream_get_meta_data()函數(shù)

代碼也只有三行:

$thisurl = "http://www.howtostagehomes.com/";
$fp = fopen($thisurl, 'r');
print_r(stream_get_meta_data($fp));

得到的結(jié)果為:

Array
(
    [wrapper_data] => Array
        (
            [0] => HTTP/1.1 200 OK
            [1] => Cache-Control: max-age=86400
            [2] => Content-Length: 76102
            [3] => Content-Type: text/html
            [4] => Content-Location: http://www.howtostagehomes.com/index.html
            [5] => Last-Modified: Fri, 19 Jul 2013 03:52:30 GMT
            [6] => Accept-Ranges: bytes
            [7] => ETag: "50bc48643384ce1:5cb3"
            [8] => Server: Microsoft-IIS/6.0
            [9] => X-Powered-By: ASP.NET
            [10] => Date: Fri, 19 Jul 2013 09:06:41 GMT
            [11] => Connection: close
        )
    [wrapper_type] => http
    [stream_type] => tcp_socket
    [mode] => r+
    [unread_bytes] => 1086
    [seekable] =>
    [uri] => http://www.howtostagehomes.com/
    [timed_out] =>
    [blocked] => 1
    [eof] =>
)

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

使用curl獲得header可以檢測(cè)GZip壓縮

先貼出代碼:

<?php
$szUrl = 'http://www.howtostagehomes.com/';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $szUrl);
curl_setopt($curl, CURLOPT_HEADER, 1);  //輸出header信息
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);  //不顯示網(wǎng)頁(yè)內(nèi)容
curl_setopt($curl, CURLOPT_ENCODING, ''); //允許執(zhí)行g(shù)zip
$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>換行符格式化輸出到網(wǎng)頁(yè)上
    echo $pHeader;
}
?>

輸出結(jié)果如下:

HTTP/1.1 200 OK
Cache-Control: max-age=86400
Content-Length: 15189
Content-Type: text/html
Content-Encoding: gzip
Content-Location: http://www.howtostagehomes.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

上面輸出結(jié)果里可以看到一個(gè)項(xiàng)目:Content-Encoding: gzip,這個(gè)正是我們用來(lái)判斷網(wǎng)頁(yè)是否啟用GZip壓縮的項(xiàng)目。

另外,需要認(rèn)真注意下本實(shí)例里的注釋部分,不能少了任何一項(xiàng),否則可能獲取header信息有誤。

標(biāo)簽: curl  壓縮  curl_init  
x
  • 站長(zhǎng)推薦
/* 左側(cè)顯示文章內(nèi)容目錄 */