如何从PHP中的Stackoverflow API读取GZIP版本的响应? [英] How can I read GZIP-ed response from Stackoverflow API in PHP?

查看:76
本文介绍了如何从PHP中的Stackoverflow API读取GZIP版本的响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从PHP中的Stackoverflow API中读取响应?响应为GZIP版本.我发现例如以下建议:

How can I read a response from Stackoverflow API in PHP? The response is GZIP-ed. I found e.g. the following suggestion:

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

,但是我正在使用的安装上没有功能http_inflate().

but the function http_inflate() is not available on the installation that I am using.

还有其他简单的方法可以实现它吗?

Are there some other easy ways to accomplish it?

推荐答案

一种很酷的方法 http://www.php.net/manual/zh/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"))));

或使用curl

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

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

edited-- other methods removed because they don't send an Accept-Encoding http header.

这篇关于如何从PHP中的Stackoverflow API读取GZIP版本的响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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