欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

php只抓取網(wǎng)頁頭的方法是什么

這篇文章主要介紹“php只抓取網(wǎng)頁頭的方法是什么”,在日常操作中,相信很多人在php只抓取網(wǎng)頁頭的方法是什么問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”php只抓取網(wǎng)頁頭的方法是什么”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比勐海網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式勐海網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋勐海地區(qū)。費(fèi)用合理售后完善,十余年實(shí)體公司更值得信賴。

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

php只抓取網(wǎng)頁頭的方法是什么

本文操作環(huán)境:windows7系統(tǒng)、PHP7.1版、DELL G3電腦

php如何只抓取網(wǎng)頁頭?

php獲取網(wǎng)頁header信息的4種方法

php獲取網(wǎng)頁header信息的方法多種多樣,就php語言來說,我知道的方法有4種, 下面逐一獻(xiàn)上。

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

推薦指數(shù): ★★★★★

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

$thisurl = "http://www.lao8.org/";
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.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

推薦指數(shù): ★★★

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

$thisurl = "http://www.lao8.org";
$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.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()函數(shù)

推薦指數(shù): ★★★

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

$thisurl = "http://www.lao8.org/";
$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.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的高級函數(shù) CURL()來獲取

推薦指數(shù): ★★★★

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

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

先貼出代碼:

<?php
$szUrl = 'http://www.lao8.org/';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $szUrl);
curl_setopt($curl, CURLOPT_HEADER, 1);  //輸出header信息
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);  //不顯示網(wǎng)頁內(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("rn", "n", "r");  //需要格式化header字符串
    $pHeader = str_replace($split, '<br>', $pHeader); //使用<br>換行符格式化輸出到網(wǎng)頁上
    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.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,網(wǎng)頁啟用了GZip壓縮。

到此,關(guān)于“php只抓取網(wǎng)頁頭的方法是什么”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!

網(wǎng)站標(biāo)題:php只抓取網(wǎng)頁頭的方法是什么
URL分享:http://www.chinadenli.net/article20/jcoojo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃手機(jī)網(wǎng)站建設(shè)微信公眾號全網(wǎng)營銷推廣網(wǎng)頁設(shè)計(jì)公司網(wǎng)站內(nèi)鏈

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)公司