Reputation: 83
I have been trying several ways, before deciding to ask this question here... no way has succeeded with me... I'm trying to decode and read the data from one site that use gzip.
I'm using cURL & PHP. When I try to decode and print the result, I'm getting a long list of garbled special characters such as:
JHWkdsU01EUXdWa1pXYTFOdFZsZFRiaz
VoVW14S2NGbFljRmRXYkdSWVpFZEdWRT
FYVWtoWmEyaExXVlpLTm1KR1VsWmlXR2
If I run the below PHP script I got an error like:
PHP Warning: gzdecode(): data error in /var/www/mn.php on line 20
Here's my current code:
<?
$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);
?>
I also try to enable deflate
module by:
a2enmod deflate
/etc/init.d/apache2 restart
and enable the zlib
from php.ini
either I try to test it directly
curl -sH 'Accept-encoding: gzip' http://example.com/getN.php&keyword=9999 | gunzip -
I got the same result.
Here is the info from the site:
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
please help
Upvotes: 2
Views: 3803
Reputation: 83
thanks all ,, finally start working after I take your advice and remove gzdecode and some others and keep the header to.. Accept Encoding to gzip and here the final code
<?
$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;
?>
Upvotes: 0
Reputation: 1852
I notice your code has
curl_setopt($ch,CURLOPT_ENCODING , 'gzip');
and a gzdecode()
call later on. If instructed to accept encoded content, cURL handles decoding automatically for you, without the need to manually do it after curl_exec()
. Its return value is already decoded if you told cURL to accept encoded transfer.
That said, the page you are trying to download may not be actually be encoded with gzip, but another method. As stated in the manual, try specifying an empty string:
# Enable all supported encoding types.
curl_setopt($ch, CURLOPT_ENCODING, '');
This enables all supported encoding types. And don't use gzdecode()
. The result should be already decoded.
Upvotes: 3