欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-03-26 11:38:43
本文实例讲述了php使用curl获取header检测开启gzip压缩的方法。分享给大家供大家参考,具体如下: 获得网页header信息,是网站开发人员和维护人员常用的技术...

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

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

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

方法一:使用get_headers()函数

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

$thisurl = "https://www.jb51.net/";
print_r(get_headers($thisurl, 1));

得到的结果为:

array
(
    [0] => http/1.1 200 ok
    [content-type] => text/html
    [last-modified] => wed, 15 aug 2018 01:23:03 gmt
    [etag] => "99a921833634d41:0"
    [server] => microsoft-iis/7.5
    [x-powered-by] => jb51.net
    [date] => wed, 15 aug 2018 01:31:48 gmt
    [connection] => close
    [content-length] => 89251
)

方法二:使用http_response_header

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

$thisurl = "https://www.jb51.net/";
$html = file_get_contents($thisurl ); 
print_r($http_response_header);

得到的结果为:

array
(
    [0] => http/1.1 200 ok
    [1] => content-type: text/html
    [2] => last-modified: wed, 15 aug 2018 01:33:04 gmt
    [3] => etag: "7b9757e93734d41:0"
    [4] => server: microsoft-iis/7.5
    [5] => x-powered-by: jb51.net
    [6] => date: wed, 15 aug 2018 01:34:15 gmt
    [7] => connection: close
    [8] => content-length: 89282
)

方法三:使用stream_get_meta_data()函数

代码也只有三行:

$thisurl = "https://www.jb51.net/";
$fp = fopen($thisurl, 'r'); 
print_r(stream_get_meta_data($fp));

得到的结果为:

array
(
    [wrapper_data] => array
        (
            [0] => http/1.1 200 ok
            [1] => content-type: text/html
            [2] => last-modified: wed, 15 aug 2018 01:38:45 gmt
            [3] => etag: "ecc8f8b43834d41:0"
            [4] => server: microsoft-iis/7.5
            [5] => x-powered-by: jb51.net
            [6] => date: wed, 15 aug 2018 01:39:35 gmt
            [7] => connection: close
            [8] => content-length: 89421
        )
    [wrapper_type] => http
    [stream_type] => tcp_socket/ssl
    [mode] => r
    [unread_bytes] => 7945
    [seekable] =>
    [uri] =>
    [timed_out] =>
    [blocked] => 1
    [eof] =>
)

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

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

先贴出代码:

<?php
$szurl = 'http://www.webkaka.com/';
$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, ''); //允许执行gzip
$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>换行符格式化输出到网页上
  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.webkaka.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

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

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

更多关于php相关内容感兴趣的读者可查看本站专题:《php curl用法总结》、《php网络编程技巧总结》、《php数组(array)操作技巧大全》、《php字符串(string)用法总结》、《php数据结构与算法教程》及《php中json格式数据操作技巧汇总

希望本文所述对大家php程序设计有所帮助。