我正在使用curl获取一个gzip压缩的网页,但当我将获取到的内容输出到浏览器时,我只得到了原始的gzip数据。如何在PHP中解码数据?
我发现的一种方法是将内容写入临时文件,然后...
$f = gzopen($filename,"r");
$content = gzread($filename,250000);
gzclose($f);
...但是,伙计们,必须有更好的方法。
编辑:这不是一个文件,而是由Web服务器返回的压缩的HTML页面。
我正在使用curl获取一个gzip压缩的网页,但当我将获取到的内容输出到浏览器时,我只得到了原始的gzip数据。如何在PHP中解码数据?
我发现的一种方法是将内容写入临时文件,然后...
$f = gzopen($filename,"r");
$content = gzread($filename,250000);
gzclose($f);
...但是,伙计们,必须有更好的方法。
编辑:这不是一个文件,而是由Web服务器返回的压缩的HTML页面。
以下命令启用cURL的“自动编码”模式,在此模式下,它将通过Accept-Encoding
头告诉服务器它支持哪些编码方法,并自动为您解压缩响应:
// Allow cURL to use gzip compression, or any other supported encoding
// A blank string activates 'auto' mode
curl_setopt($ch, CURLOPT_ENCODING , '');
如果你想要强制使用头部信息Accept-Encoding: gzip
,可以使用以下命令:// Allow cURL to use gzip compression, or any other supported encoding
curl_setopt($ch, CURLOPT_ENCODING , 'gzip');
阅读更多,请查看PHP文档:curl_setopt。
感谢评论者帮助改进此答案。
'gzip'
将始终发送Accept-Encoding: gzip
,即使您的PHP版本不支持解压缩gzip(那么您将获取压缩数据)。如果您将其设置为空字符串''
,curl将自动宣告并解码它支持的所有编码。 - AndreKRfunction gunzip($zipped) { $offset = 0; if (substr($zipped,0,2) == "\x1f\x8b") $offset = 2; if (substr($zipped,$offset,1) == "\x08") { # file_put_contents("tmp.gz", substr($zipped, $offset - 2)); return gzinflate(substr($zipped, $offset + 8)); } return "未知格式"; }将该函数与CURL集成的示例:
$headers_enabled = 1; curl_setopt($c, CURLOPT_HEADER, $headers_enabled) $ret = curl_exec($c);
if ($headers_enabled) { # file_put_contents("preungzip.html", $ret);
$sections = explode("\x0d\x0a\x0d\x0a", $ret, 2); while (!strncmp($sections[1], 'HTTP/', 5)) { $sections = explode("\x0d\x0a\x0d\x0a", $sections[1], 2); } $headers = $sections[0]; $data = $sections[1];
if (preg_match('/^Content-Encoding: gzip/mi', $headers)) { printf("找到gzip头\n"); return gunzip($data); } }
return $ret;