I'm retrieving a gzipped web page via curl, but when I output the retrieved content to the browser I just get the raw gzipped data. How can I decode the data in PHP?
One method I found was to write the content to a tmp file and then ...
$f = gzopen($filename,"r");
$content = gzread($filename,250000);
gzclose($f);
.... but man, there's got to be a better way.
Edit: This isn't a file, but a gzipped html page returned by a web server.
I use curl and:
curl_setopt($ch,CURLOPT_ENCODING , "gzip");
Just to note that this option sets the
Accept-Encoding: gzip
header on the request and uncompresses the response if it is compressed (it may not be), so it is indeed all you need to do.Perfect solution for CURL.
Setting it to
'gzip'
will always sendAccept-Encoding: gzip
, even when your PHP version doesn't support decoding gzip (you'll get the compressed data then). If you set it to''
(empty string), curl will automatically announce and decode all encodings that it supports.