2014-02-27 114 views
1

我试图从http://api.stackoverflow.com/1.1/search?tagged=php获取数据。无法从Stack Exchange API获取数据

我使用这个代码从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来获取数据,但它也没有显示任何内容。为什么它没有向我展示任何东西,我该如何解决这个问题?

+0

当你点击链接您能得到什么? – snollygolly

回答

3

他们正在返回你压缩的内容作为回应。这就是为什么它不适用于你的json解码。这里是等同的卷曲请求。

$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 
+1

打我几秒钟:)但我可以证实,这工作。 –

1

的响应gzip压缩,使用gzdecode

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