首頁  >  文章  >  後端開發  >  php如何只抓取網頁頭

php如何只抓取網頁頭

藏色散人
藏色散人原創
2021-10-14 09:16:201866瀏覽

php只抓取網頁頭的方法:1、使用get_headers()函數;2、使用http_response_header方法;3、使用stream_get_meta_data()函數;4、使用php CURL來取得網頁頭即可。

php如何只抓取網頁頭

本文操作環境:windows7系統、PHP7.1版、DELL G3電腦

php如何只抓取網頁頭?

php取得網頁header資訊的4種方法

php取得網頁header資訊的方法多種多樣,就php語言來說,我知道的方法有4種, 下面逐一獻上。

方法一:使用get_headers()函數

推薦指數: ★★★★★★

get_header方法最簡單只要兩行程式碼即可搞定。如下:

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

得到的結果為:

Array
(
    [0] => HTTP/1.1 200 OK
    [Cache-Control] => max-age=86400
    [Content-Length] => 76102
    [Content-Type] => text/html
    [Content-Location] => http://www.lao8.org/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

推薦指數: ★★★

http_response_headerf方法也很簡單,僅三行:

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

得到的結果為:

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.lao8.org/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()函數

推薦指數: ★★★

使用stream_get_meta_data()程式碼也只需三行:

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

得到的結果為:

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.lao8.org/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.lao8.org/
    [timed_out] => 
    [blocked] => 1
    [eof] => 
)

第四種方法: 使用php的高階函數CURL()來取得

推薦指數: ★★★★

上面的三種方法能獲取一般的網頁header信息,如果想要獲取更詳細的header信息比如網頁是否啟用了GZip壓縮。這時候可以用php的高階函數curl()來取得。

使用curl取得header可以偵測GZip壓縮

先貼出程式碼:

<?php
$szUrl = &#39;http://www.lao8.org/&#39;;
$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, &#39;&#39;); //允许执行gzip
$data=curl_exec($curl); 
if(!curl_errno($curl))
{
    $info = curl_getinfo($curl);
    $httpHeaderSize = $info[&#39;header_size&#39;];  //header字符串体积
    $pHeader = substr($data, 0, $httpHeaderSize); //获得header字符串
    $split   = array("rn", "n", "r");  //需要格式化header字符串
    $pHeader = str_replace($split, &#39;<br>&#39;, $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.lao8.org/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

 

可以看到使用curl取得到的header資訊多了這行:Content-Encoding: gzip,網頁啟用了GZip壓縮。

推薦學習:《PHP影片教學

以上是php如何只抓取網頁頭的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn