当前位置:首页 > PHP教程 > php高级应用 > 列表

php使用curl获取header检测开启GZip压缩的方法

发布:smiling 来源: PHP粉丝网  添加日期:2021-10-23 14:32:12 浏览: 评论:0 

这篇文章主要介绍了php使用curl获取header检测开启GZip压缩的方法,结合实例形式总结分析了三种常见的header检测方法并给出了使用curl检测Gzip压缩开启情况的方法,需要的朋友可以参考下。

本文实例讲述了php使用curl获取header检测开启GZip压缩的方法,分享给大家供大家参考,具体如下:

获得网页header信息,是网站开发人员和维护人员常用的技术。网页的header信息,非常丰富,非专业人士一般较难读懂和理解各个项目的含义。

获取网页header信息,方法多种多样,就php语言来说,我作为一个菜鸟,知道的方法也有4种那么多。下面逐一献上。

方法一:使用get_headers()函数

这个方法很多人使用,也很简单便捷,只需要两行代码即可搞定。如下:

$thisurl = "https://www.phpfensi.com/";

print_r(get_headers($thisurl, 1));

得到的结果为:

  1. Array 
  2.     [0] => HTTP/1.1 200 OK 
  3.     [Content-Type] => text/html 
  4.     [Last-Modified] => Wed, 15 Aug 2018 01:23:03 GMT 
  5.     [ETag] => "99a921833634d41:0" 
  6.     [Server] => Microsoft-IIS/7.5 
  7.     [X-Powered-By] => jb51.net 
  8.     [Date] => Wed, 15 Aug 2018 01:31:48 GMT 
  9.     [Connection] => close 
  10.     [Content-Length] => 89251 

方法二:使用http_response_header

代码也很简单,仅需三行:

  1. $thisurl = "https://www.phpfensi.com/"
  2. $html = file_get_contents($thisurl );  
  3. print_r($http_response_header); 

得到的结果为:

  1. Array 
  2.     [0] => HTTP/1.1 200 OK 
  3.     [1] => Content-Type: text/html 
  4.     [2] => Last-Modified: Wed, 15 Aug 2018 01:33:04 GMT 
  5.     [3] => ETag: "7b9757e93734d41:0" 
  6.     [4] => Server: Microsoft-IIS/7.5 
  7.     [5] => X-Powered-By: jb51.net 
  8.     [6] => Date: Wed, 15 Aug 2018 01:34:15 GMT 
  9.     [7] => Connection: close 
  10.     [8] => Content-Length: 89282 

方法三:使用stream_get_meta_data()函数

代码也只有三行:

  1. $thisurl = "https://www.phpfensi.com/"
  2. $fp = fopen($thisurl'r');  
  3. print_r(stream_get_meta_data($fp)); 

得到的结果为:

  1. Array 
  2.     [wrapper_data] => Array 
  3.         ( 
  4.             [0] => HTTP/1.1 200 OK 
  5.             [1] => Content-Type: text/html 
  6.             [2] => Last-Modified: Wed, 15 Aug 2018 01:38:45 GMT 
  7.             [3] => ETag: "ecc8f8b43834d41:0" 
  8.             [4] => Server: Microsoft-IIS/7.5 
  9.             [5] => X-Powered-By: jb51.net 
  10.             [6] => Date: Wed, 15 Aug 2018 01:39:35 GMT 
  11.             [7] => Connection: close 
  12.             [8] => Content-Length: 89421 
  13.         ) 
  14.     [wrapper_type] => http 
  15.     [stream_type] => tcp_socket/ssl 
  16.     [mode] => r 
  17.     [unread_bytes] => 7945 
  18.     [seekable] => 
  19.     [uri] => https://www.phpfensi.com/ 
  20.     [timed_out] => 
  21.     [blocked] => 1 
  22.     [eof] => 

上述三种方法都可以轻松获得网页header信息,且包含的信息都已经相当丰富,满足一般要求,不过比较遗憾的是,上述三种方法都不能用来检测网页是否启用了GZip压缩。要检测GZip压缩,还需其他的方法才行。这里介绍的是用curl()函数来检测。

使用curl获得header可以检测GZip压缩

先贴出代码:

  1. <?php 
  2. $szUrl = 'http://www.webkaka.com/'
  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.   $info = curl_getinfo($curl); 
  11.   $httpHeaderSize = $info['header_size']; //header字符串体积 
  12.   $pHeader = substr($data, 0, $httpHeaderSize); //获得header字符串 
  13.   $split  = array("\r\n""\n""\r"); //需要格式化header字符串 
  14.   $pHeader = str_replace($split'<br>'$pHeader); //使用<br>换行符格式化输出到网页上 
  15.   echo $pHeader
  16. ?> 

输出结果如下:

  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.webkaka.com/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 

上面输出结果里可以看到一个项目:Content-Encoding: gzip,这个正是我们用来判断网页是否启用GZip压缩的项目。

另外,需要认真注意下本实例里的注释部分,不能少了任何一项,否则可能获取header信息有误。

Tags: curl header GZip

分享到: