无法从Stack Exchange API获取数据 [英] Can't get data from Stack Exchange API

查看:111
本文介绍了无法从Stack Exchange API获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 http://api.stackoverflow.com/1.1/search?tagged= php .

我正在使用以下代码从API中获取数据:

I am using this code to get data from the API:

$url = "http://api.stackoverflow.com/1.1/search?tagged=php";
$json = file_get_contents($url);
$json_data = json_decode($json, true);
print_r($json);

但是它什么也没给我显示.我还使用curl来获取数据,但也什么也没显示.为什么它没有显示任何内容,我该如何解决?

But it is showing me nothing. I have also used curl to get the data, but it also shows nothing. Why isn't it showing me anything, and how can I fix that?

推荐答案

他们将您压缩后的内容作为响应返回给您.这就是为什么它不能与您的json解码一起使用的原因.这是等效的curl请求.

They are returning you gzipped content as response. That's why it didn't work with your json decoding. Here is equivalent curl request.

$url= "http://api.stackoverflow.com/1.1/search?tagged=php";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_ENCODING, ""); // this will handle gzip content
$result = curl_exec($ch);
curl_close($ch);
print $result;
// do json processing here

这篇关于无法从Stack Exchange API获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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