无法使用cURL PHP解码gzip

无法使用cURL PHP解码gzip,php,curl,gzip,deflate,Php,Curl,Gzip,Deflate,在决定在这里问这个问题之前,我一直在尝试几种方法。。。我没有办法成功。。。我试图解码和读取一个使用gzip的站点的数据 我正在使用cURL和PHP。当我尝试解码并打印结果时,我会得到一长串乱码特殊字符,例如: JHWkdsU01EUXdWa1pXYTFOdFZsZFRiaz VoVW14S2NGbFljRmRXYkdSWVpFZEdWRT FYVWtoWmEyaExXVlpLTm1KR1VsWmlXR2 如果我运行下面的PHP脚本,会出现如下错误: PHP Warning: gzde

在决定在这里问这个问题之前,我一直在尝试几种方法。。。我没有办法成功。。。我试图解码和读取一个使用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);


?>
并从
php.ini

要么我试着直接测试它

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
请帮助

我注意到您的代码已被删除

curl_setopt($ch,CURLOPT_ENCODING , 'gzip');
以及稍后的
gzdecode()
调用。如果指示接受编码内容,cURL将自动为您处理解码,而无需在
cURL\u exec()之后手动执行。如果您告诉cURL接受编码传输,它的返回值已经被解码

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

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

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

谢谢大家,在我接受你们的建议并删除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;


?>


example.com中的实际数据似乎不是gzip,因此您很难对此进行调试。我尝试了使用/不使用(gzip、gzdecode、Accept Encoding:gzip/deflate)的方法,但都给出了相同的结果(一长串乱码特殊字符)或错误(PHP警告:gzdecode():行号中的数据错误)我还复制了gzip代码,并尝试手动解码它,或者不工作。。。bwt。。我有这个网站,他们在线解码我的gzip代码没有任何问题..工作!!因此,请在我的代码或服务器设置中确认此问题。(我的服务器未解码)。您不必自己设置内容长度。如果使用cURL发送HTTP POST,它将为您计算内容长度,并自动添加所需的内容长度头。如果cURL压缩内容,它可能小于strlen($data\u string)
。另外,完全删除CURLOPT_HTTPHEADER,并将CURLOPT_POST选项设置为true。
<?
$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;


?>