获取 json 内容时出现问题

发布于 2024-11-14 18:16:16 字数 636 浏览 4 评论 0原文

嗨,我正在尝试从 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();我也没有得到任何东西,只有问题和问题

Hi i'm trying to get the content from a json file, but i can't i have many troubles to do it, my code is:

<?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);  

?>

if i put the address in the browser i get the content without any issue, but if i try to get it on PHP or other code i have issues, what can i do? i try use file_get_contents(); too but i don't get anything, only issues and issues

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜嗑 2024-11-21 18:16:16

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

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

It appears that they are checking user agents and blocking PHP. Set this before using file_get_contents:

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

If they are checking user agents, they may be doing so to prevent people from doing this kind of thing.

非要怀念 2024-11-21 18:16:16

您在 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

Have you read a little bit about cURL on php.net?

here is how it works:

From 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文