我'使用curl检查是否有任何给定的网站启用了gzip。 我使用以下代码进行检查。
$ch = curl_init('website name');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Encoding: gzip, deflate',
'Accept-Language: en-US,en;q=0.5',
'Connection: keep-alive',
'SomeBull: BeingIgnored',
'User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:16.0) Gecko/20100101 Firefox/16.0'
)
);
$response = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
print_r($info);
每次结果都会显示结果中包含的gzip选项。甚至那些没有像http://pramitscomicsdump.com
那样启用gzip的网站也可以告诉我我做错了什么。我只需要使用curl检查是否启用了gzip。
我学会了用这个命令来检查
curl -I -H 'Accept-Encoding: gzip,deflate' 'site name'
但是我无法在php中运行此命令。
答案 0 :(得分:1)
努力使用您的代码。
检查时需要考虑以下几点:
CURLOPT_FOLLOWLOCATION
。如果您被重定向,cURL将返回多组标题,因此您需要检查最终请求中的标题。以下是一些可以帮助您入门的代码:
<?php
$ch = curl_init('http://example.com');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // follow redirects
curl_setopt($ch, CURLOPT_HEADER, 1); // include headers in curl response
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Encoding: gzip, deflate', // request gzip
'Accept-Language: en-US,en;q=0.5',
'Connection: keep-alive',
'SomeBull: BeingIgnored',
'User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:16.0) Gecko/20100101 Firefox/16.0'
)
);
$response = curl_exec($ch);
if ($response === false) {
die('Error fetching page: ' . curl_error($ch));
}
$info = curl_getinfo($ch);
for ($i = 0; $i <= $info['redirect_count']; ++$i) {
// split request and headers into separate vars for as many times
// as there were redirects
list($headers, $response) = explode("\r\n\r\n", $response, 2);
}
curl_close($ch);
$headers = explode("\r\n", $headers); // split headers into one per line
$hasGzip = false;
foreach($headers as $header) { // loop over each header
if (stripos($header, 'Content-Encoding') !== false) { // look for a Content-Encoding header
if (strpos($header, 'gzip') !== false) { // see if it contains gzip
$hasGzip = true;
}
}
}
var_dump($hasGzip);