php獲取網頁header信息的4種方法


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

方法一:使用get_headers()函數

推薦指數: ★★★★★

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

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

得到的結果為:

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

方法二:使用http_response_header

推薦指數: ★★★

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

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

得到的結果為:

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

方法三:使用stream_get_meta_data()函數

推薦指數: ★★★

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

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

得到的結果為:

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

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

推薦指數: ★★★★

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

使用curl獲得header可以檢測GZip壓縮
先貼出代碼:

  1. <?php
  2. $szUrl = 'http://www.lao8.org/';
  3. $curl = curl_init();
  4. curl_setopt($curl, CURLOPT_URL, $szUrl);
  5. curl_setopt($curl, CURLOPT_HEADER, 1);  //輸出header信息
  6. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);  //不顯示網頁內容
  7. curl_setopt($curl, CURLOPT_ENCODING, ''); //允許執行gzip
  8. $data=curl_exec($curl); 
  9. if(!curl_errno($curl))
  10. {
  11.     $info = curl_getinfo($curl);
  12.     $httpHeaderSize = $info['header_size'];  //header字符串體積
  13.     $pHeader = substr($data, 0, $httpHeaderSize); //獲得header字符串
  14.     $split   = array("rn", "n", "r");  //需要格式化header字符串
  15.     $pHeader = str_replace($split, '<br>', $pHeader); //使用<br>換行符格式化輸出到網頁上
  16.     echo $pHeader;
  17. }
  18. ?>

輸出結果如下:

 

  1. HTTP/1.1 200 OK
  2. Cache-Control: max-age=86400
  3. Content-Length: 15189
  4. Content-Type: text/html
  5. Content-Encoding: gzip
  6. Content-Location: http://www.lao8.org/index.html
  7. Last-Modified: Fri, 19 Jul 2013 03:52:28 GMT
  8. Accept-Ranges: bytes
  9. ETag: "0268633384ce1:5cb3"
  10. Vary: Accept-Encoding
  11. Server: Microsoft-IIS/6.0
  12. X-Powered-By: ASP.NET
  13. Date: Fri, 19 Jul 2013 09:27:21 GMT

 

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM