本示例详细讲解如何利用PHP的curl功能获取HTTP Header,进而检测网站是否开启了GZip压缩。内容供开发者参考与借鉴。
获取网页Header数据是网站开发与运维中的常用技能。Header中包含大量元数据,对于非专业人员而言,理解各项参数的具体含义往往存在一定难度。
在PHP语言中,获取Header信息的手段多样。作者列举了四种常见方法,并逐一进行说明。
第一种方案:调用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 2026 01:23:03 GMT [ETag] => "99a921833634d41:0" [Server] => Microsoft-IIS/7.5 [X-Powered-By] => jb51.net [Date] => Wed, 15 Aug 2026 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 2026 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 2026 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 2026 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 2026 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] => https://www.jb51.net/ [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, '
', $pHeader); //使用
换行符格式化输出到网页上
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开发者有所助益。