Php curl, response detection - gzip or not

I use curl to retrieve data from many websites. Sometimes they send gzip back. How to determine gzip answer or not?

+3
source share
2 answers
curl_setopt($curl, CURLOPT_HEADER, true);

The result curl_execwill also include HTTP response headers, which you can parse to see if it has been gzipped.

Gzipped's answer would be:

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 the response into headers and content, and then analyze the headers to see if it is talking about gzip-encoded content.

+3
source

BTW if you installed:

CURLOPT_ENCODING => '',

CURL will automatically decrypt the data for you.

+5
source

Source: https://habr.com/ru/post/1776986/


All Articles