StoneHeart
StoneHeart

Reputation: 16610

php curl, detect response is gzip or not

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

Upvotes: 3

Views: 3257

Answers (2)

Alix Axel
Alix Axel

Reputation: 154671

BTW, if you set:

CURLOPT_ENCODING => '',

CURL will automatically decode the data for you.

Upvotes: 6

Jon
Jon

Reputation: 437804

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.

Upvotes: 3

Related Questions