【问题标题】:issues getting json content获取 json 内容的问题
【发布时间】:2011-06-09 05:26:25
【问题描述】:

您好,我正在尝试从 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();也是,但我什么也没得到,只有问题和问题

【问题讨论】:

  • 定义术语问题
  • 是的,我得到:403 - 禁止

标签: php json api curl file-get-contents


【解决方案1】:

他们似乎正在检查用户代理并阻止 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');

如果他们检查用户代理,他们这样做可能是为了防止人们做这种事情。

【讨论】:

    【解决方案2】:

    你在 php.net 上读过一些关于 cURL 的文章吗?

    这是它的工作原理:

    来自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
    

    【讨论】:

    • 是的,我已经阅读过相关内容,但我仍然收到“403 - Forbidden”错误
    • 调试尝试使用 file_get_contents($url);回应它
    • 你应该看看 api 的文档,他们一定是阻塞了一些调用
    猜你喜欢
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    • 2014-09-15
    • 1970-01-01
    • 2021-08-04
    • 1970-01-01
    • 2011-06-29
    • 1970-01-01
    相关资源
    最近更新 更多