解压缩gzip压缩的HTTP响应 [英] Uncompress gzip compressed http response

查看:106
本文介绍了解压缩gzip压缩的HTTP响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用php的file_get_contents()函数执行HTTP请求.为了节省带宽,我决定使用stream_context_create()添加"Accept-Encoding: gzip"标头.

I'm using php's file_get_contents() function to do a HTTP request. To save bandwidth I decided to add the "Accept-Encoding: gzip" header using stream_context_create().

很明显,file_get_contents()输出一个gzip编码的字符串,因此我正在使用gzuncompress()解码该编码的字符串,但是将作为参数传递的数据出错.

Obviously, file_get_contents() outputs a gzip encoded string so I'm using gzuncompress() to decode the encoded string but I get an error with data passed as argument.

[...] PHP Warning: gzuncompress(): data error in /path/to/phpscript.php on line 26

我知道还有另一个功能可以解压缩压缩后的数据gzdecode(),但是它不包含在我的PHP版本中(也许仅在SVN上可用).

I know there is another function able to decompress gzipped data gzdecode() but it isn't included in my PHP version (maybe it is only available on SVN).

我知道cUrl可以即时解码gzip流(没有任何问题),但是有人建议我使用file_get_contents()而不是cUrl.

I know that cUrl decodes gzip stream on the fly (without any problem) but someone suggested me to use file_get_contents() instead of cUrl.

您是否知道以其他方式解压缩PHP中压缩的数据,或者为什么gzuncompress()输出警告?荒谬的是gzuncompress()不能按预期工作.

Do you know any other way to decompress gzipped data in PHP or why gzuncompress() outputs a Warning? It is absurd that gzuncompress() doesn't work as expected.

注意: 问题肯定与PHP有关:HTTP请求是针对Tumblr API发出的,它给出了编码正确的响应.

Notes: The problem is certainly about PHP: the HTTP request is made to Tumblr API that give a well-encoded response.

推荐答案

gzuncompress 不适用于gzip编码.这是.Z存档文件的解压缩功能.

gzuncompress won't work for the gzip encoding. It's the decompression function for the .Z archives.

该手册列出了缺少的 gzdecode()#82930 的一些解决方法,或者仅使用一种通过 upgradephp 或gzopen临时文件的解决方法.

The manual lists a few workarounds for the missing gzdecode()#82930, or just use the one from upgradephp, or the gzopen temp file workaround.

另一个选择是使用Accept-Encoding:标头强制deflate编码,然后使用 gzinflate() 进行减压.

Another option would be forcing the deflate encoding with the Accept-Encoding: header and then using gzinflate() for decompression.

这篇关于解压缩gzip压缩的HTTP响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆