【问题标题】:Unable to decode gzip using cURL PHP无法使用 cURL PHP 解码 gzip
【发布时间】:2015-10-15 01:46:13
【问题描述】:

在决定在这里问这个问题之前,我已经尝试了几种方法...我没有成功...我正在尝试从一个使用 gzip 的站点解码和读取数据。

我正在使用 cURL 和 PHP。当我尝试解码并打印结果时,我得到一长串乱码的特殊字符,例如:

 JHWkdsU01EUXdWa1pXYTFOdFZsZFRiaz
 VoVW14S2NGbFljRmRXYkdSWVpFZEdWRT
 FYVWtoWmEyaExXVlpLTm1KR1VsWmlXR2

如果我运行下面的 PHP 脚本,我会收到如下错误:

 PHP Warning:  gzdecode(): data error in /var/www/mn.php on line 20

这是我当前的代码:

<?
$data_string = '9999';
$ch = curl_init('http://example.com/getN.php&keyword=');
curl_setopt( $ch, CURLOPT_USERAGENT, 'Darwin/15.0.0' );
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch,CURLOPT_ENCODING , 'gzip');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_TIMEOUT,5);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); // Follow redirects
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/x-www-form-urlencoded',
    'Accept-Encoding: gzip, deflate',
    'Content-Length: ' . strlen($data_string))
);


$result = gzdecode ( curl_exec($ch) );

curl_close($ch);
print_r($result);


?>

我也尝试通过以下方式启用deflate 模块:

  a2enmod deflate
  /etc/init.d/apache2 restart

并从php.ini启用zlib

要么我尝试直接测试它

curl -sH 'Accept-encoding: gzip' http://example.com/getN.php&keyword=9999 | gunzip - 

我得到了同样的结果。

这是来自网站的信息:

HTTP/1.1 200 OK
Server: nginx
Date: Thu, 15 Oct 2015 00:41:54 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Vary: Accept-Encoding
X-Powered-By: PHP/5.4.31
X-Frame-Options: SAMEORIGIN
Content-Encoding: gzip

请帮忙

【问题讨论】:

  • 很难为您调试,因为来自 example.com 的实际数据似乎没有被压缩

标签: php curl gzip deflate


【解决方案1】:

我注意到你的代码有

curl_setopt($ch,CURLOPT_ENCODING , 'gzip');

稍后再拨打gzdecode()。如果指示接受编码内容,cURL 会自动为您处理解码,而无需在curl_exec() 之后手动进行。如果您告诉 cURL 接受编码传输,它的返回值已经被解码。

也就是说,您尝试下载的页面实际上可能不是用 gzip 编码的,而是另一种方法。如manual 中所述,尝试指定一个空字符串:

# Enable all supported encoding types.
curl_setopt($ch, CURLOPT_ENCODING, '');

这会启用所有支持的编码类型。并且不要使用gzdecode()。结果应该已经解码了。

【讨论】:

  • 我试过有/没有(gzip,gzdecode,Accept-Encoding:gzip/deflate)都给我相同的结果(乱码特殊字符的长列表)或错误(PHP警告:gzdecode( ):行号中的数据错误)我还复制了gzip代码并尝试手动解码它不起作用... bwt ..我有这个link网站他们在线解码我的gzip代码没有任何问题..工作!!以便在我的代码或服务器设置中确认我这个问题。 (我的服务器没有解码)。
  • 您不必自己设置 Content-Length。如果您使用 cURL 发送 HTTP POST,它会为您计算内容长度,并自动添加所需的 Content-Length 标头。如果 cURL 压缩内容,它可能小于 strlen($data_string)。此外,完全删除 CURLOPT_HTTPHEADER,并将 CURLOPT_POST 选项设置为 true。
【解决方案2】:

谢谢大家,在我听取您的建议并删除 gzdecode 和其他一些内容并保留标题后终于开始工作了。接受对 gzip 的编码,这里是最终代码

<?
$data_string = '9999';
$ch = curl_init('http://example.com/getN.php&keyword=');
curl_setopt( $ch, CURLOPT_USERAGENT, 'Darwin/15.0.0' );
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_TIMEOUT,5);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); // Follow redirects
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/x-www-form-urlencoded',
'Accept-Encoding: gzip',
'Content-Length: ' . strlen($data_string))
);


$result = curl_exec($ch);

curl_close($ch);
print $result;


?>

【讨论】:

    猜你喜欢
    • 2015-01-17
    • 2020-06-05
    • 2011-03-28
    • 2014-01-13
    • 1970-01-01
    • 2015-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多