3

I'm using curl to fetch data from many website. Sometimes they send back gzip. How to detect response is gzip or not?

1

2 Answers 2

6

BTW, if you set:

CURLOPT_ENCODING => '',

CURL will automatically decode the data for you.

3
curl_setopt($curl, CURLOPT_HEADER, true);

The result of curl_exec will then also include the headers from the HTTP response, which you can parse to see if it is gzipped.

A gzipped response would be like this:

HTTP/1.1 200 OK
Server: Apache
Content-Type: text/html
Content-Encoding: gzip
Content-Length: 26395

[GZIP COMPRESSED DATA]

So, you can first break up the response into headers and contents and then parse the headers to see if it says anything about gzip encoded content.

Not the answer you're looking for? Browse other questions tagged or ask your own question.