php curl, detect response is gzip or not
I'm using curl to fetch data from many website. Somet开发者_开发技巧imes they send back gzip. How to detect response is gzip or not?
BTW, if you set:
CURLOPT_ENCODING => '',
CURL will automatically decode the data for you.
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.
精彩评论