2011-06-09 93 views
0

嗨,我想从一个JSON文件中的内容,但我不能,我有许多麻烦做到这一点,我的代码是:问题越来越JSON内容

<?PHP 

$url = 'http://www.taringa.net/api/efc6d445985d5c38c5515dfba8b74e74/json/Users-GetUserData/apptastico'; 

$ch = curl_init(); 
$timeout = 0; // set to zero for no timeout 
curl_setopt ($ch, CURLOPT_URL, $url); 
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); 
$file_contents = curl_exec($ch); // take out the spaces of curl statement!! 
curl_close($ch); 

var_dump($file_contents); 

?> 

如果我把地址在浏览器中我得到的内容没有任何问题,但如果我尝试在PHP或其他代码上获得它,我有问题,我该怎么办?我尝试使用file_get_contents();但我没有得到任何东西,只有问题和问题

+2

定义术语_issues_ – 2011-06-09 05:30:12

+0

您是否收到一些错误? – Ibu 2011-06-09 05:35:03

+0

是的,我得到了:403 - 禁止 – ozonostudio 2011-06-13 18:47:07

回答

0

看来他们正在检查用户代理和阻止PHP。使用的file_get_contents之前设置这样的:

ini_set('user_agent', 'Mozilla/5.0 (Windows NT 5.2; rv:2.0.1) Gecko/20100101 Firefox/4.0.1'); 

如果他们正在检查用户代理,他们可能会这么做,以防止有人做这种事情。

+0

真棒!这解决了我的问题 – ozonostudio 2011-06-13 18:52:21

0

你读了一点关于cURL php.net?

这里是如何工作的:

php.net:

$url = 'http://www.taringa.net/api/efc6d445985d5c38c5515dfba8b74e74/json/Users-GetUserData/apptastico'; 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_HEADER, FALSE); // set to true to see the header 
curl_setopt($ch, CURLOPT_NOBODY, FALSE); // show the body 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
$content = curl_exec($ch); 
curl_close($ch); 

echo $content 
+0

是的,我已经读过,但我仍然得到一个“403 - 禁止”的错误 – ozonostudio 2011-06-13 18:46:51

+0

调试尝试使用file_get_contents($ url);回声它 – Ibu 2011-06-13 18:56:23

+0

你应该看看API的文档,他们必须阻止一些电话 – Ibu 2011-06-13 19:05:16