简体   繁体   English

PHP - 检测gzip服务器响应

[英]PHP - Detect gzip server response

I'm using curl to fetch a webpage, I need to detect if the response is gzip or not. 我正在使用curl来获取网页,我需要检测响应是否是gzip。

This works perfectly fine if Content-Encoding is specified in the response headers, but some servers instead return "Transfer-Encoding": "Chunked" and no Content-Encoding header. 如果在响应头中指定了Content-Encoding,则此工作完全正常,但某些服务器返回“Transfer-Encoding”:“Chunked”且没有Content-Encoding标头。

Is there any way to detect gzip or get the raw (encoded) server response? 有没有办法检测gzip或获取原始(编码)服务器响应?

I tried looking at curl_getinfo but the content_encoding isn't specified either. 我试着查看curl_getinfo但是也没有指定content_encoding。

Thanks. 谢谢。

您可以检查响应是否以gzip幻数开始,特别是1f 8b

You can either issue a separate HEAD request: 您可以发出单独的HEAD请求:

CURLOPT_HEADER => true
CURLOPT_NOBODY => true

Or request the header to be prefixed to your original request: 或者请求标头作为原始请求的前缀:

CURLOPT_HEADER => true

But, if you just want to get the (decoded) HTML, you can use: 但是,如果您只想获取(已解码的)HTML,则可以使用:

CURLOPT_ENCODING => ''

And CURL will automatically negotiate with the server and decode it for you. CURL将自动与服务器协商并为您解码。

Is there any way to detect gzip 有没有办法检测gzip

Yes. 是。 You can use cURLs Header functions. 您可以使用cURLs标头功能。 For example you can define an function, which handles the header responses. 例如,您可以定义一个处理标题响应的函数。 Use curl_setopt() with the CURLOPT_HEADERFUNCTION option. curl_setopt()CURLOPT_HEADERFUNCTION选项一起使用。 Or write it to an file (which you have created with fopen() ) with the CURLOPT_WRITEHEADER option. 或者使用CURLOPT_WRITEHEADER选项将其写入文件(使用fopen()创建的文件)。

There may are more options you could use. 您可以使用更多选项。 Look out the possibilities at the curl_setopt() manual. curl_setopt()手册中查看可能性。 The header you are looking for have the name: Content-Encoding . 您要查找的标题名称为: Content-Encoding

If you have the output in a file, you could also use PHPs finfo with some of its predefined constants . 如果你有一个文件中的输出,你也可以使用PHPs finfo及其一些预定义的常量 Or mime_content_type() ( DEPRECATED! ) if finfo is not available to you. 或者mime_content_type()DEPRECATED! )如果你没有finfo。

[...] or get the raw (encoded) server response? [...]或获取原始(编码)服务器响应?

Yes. 是。 You can specify the accept-encoding header. 您可以指定accept-encoding标头。 The value you are look for is identity . 您要寻找的价值是身份 So you can send: 所以你可以发送:

Accept-Encoding: identity

May have look to the HTTP/1.1 RFC To get an unencoded/uncompressed output (for example to directly write it into a file). 可以查看HTTP / 1.1 RFC获取未编码/未压缩的输出(例如直接将其写入文件)。 Use CURLOPT_ENCODING for this purpose. 为此目的使用CURLOPT_ENCODING You can set it also with *curl_setopt*. 您也可以使用* curl_setopt *进行设置。

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

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