如何从PHP中的Stackoverflow API读取GZIP-ed响应?

Jir*_*riz 8 php gzip

如何从PHP中读取Stackoverflow API的响应?回复是GZIP-ed.我发现了以下建议:

$url = "http://api.stackoverflow.com/1.1/questions/" . $question_id;
$data = file_get_contents($url);
$data = http_inflate($data);
Run Code Online (Sandbox Code Playgroud)

但该功能http_inflate()在我正在使用的安装上不可用.

还有其他一些简单的方法来实现它吗?

goa*_*oat 19

一个很酷的方式 http://www.php.net/manual/en/wrappers.compression.php

请注意使用流包装器compress.zlib

$url = "compress.zlib://http://api.stackoverflow.com/1.1/questions/" . $question_id; 
echo $data = file_get_contents($url, false, stream_context_create(array('http'=>array('header'=>"Accept-Encoding: gzip\r\n"))));
Run Code Online (Sandbox Code Playgroud)

或使用卷曲

$ch = curl_init();
curl_setopt_array($ch, array(
    CURLOPT_URL => $url
  , CURLOPT_HEADER => 0
  , CURLOPT_RETURNTRANSFER => 1
  , CURLOPT_ENCODING => 'gzip'
));
echo curl_exec($ch);
Run Code Online (Sandbox Code Playgroud)

编辑 - 删除其他方法,因为他们不发送Accept-Encodinghttp标头.