简体   繁体   English

php curl,检测响应是否为gzip

[英]php curl, detect response is gzip or not

I'm using curl to fetch data from many website. 我正在使用curl从许多网站获取数据。 Sometimes they send back gzip. 有时他们会发回gzip。 How to detect response is gzip or not? 如何检测响应是否是gzip?

BTW, if you set: 顺便说一句,如果你设置:

CURLOPT_ENCODING => '',

CURL will automatically decode the data for you. CURL会自动为您解码数据。

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. 然后curl_exec的结果还将包含HTTP响应的标头,您可以解析它以查看它是否被gzip压缩。

A gzipped response would be like this: 一个gzipped响应将是这样的:

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. 因此,您可以先将响应分解为标题和内容,然后解析标题以查看它是否说明了gzip编码内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM