通过curl从url获取JSON数据 [英] get JSON data from url via curl

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

问题描述

我正在尝试通过curl连接从URL获取JSON数据。当我打开链接时:它显示{ version: N / A, success:true, status:true}。

现在,我想获得以上内容。

I am trying to get JSON data from the url via curl connection. When I open the link: it shows {"version":"N/A","success":true,"status":true}.
Now, I want to get above content.

到目前为止,我已经使用过:

So far I have use this:

$loginUrl = 'http://update.protect-website.com/index.php?plugin=firewall&action=getVersion';
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$loginUrl);
$result=curl_exec($ch);
curl_close($ch);
var_dump(json_decode($result));

但是,我总是得到NULL,有人知道哪里错了吗?

However, I always get NULL, Does someone have idea where is wrong?

推荐答案

网站检查用户代理。添加代理选项,它将起作用。

The website check the user agent. Add an agent option, and it will work.

$loginUrl = 'http://update.protect-website.com/index.php?plugin=firewall&action=getVersion';
$agent= 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)';

$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$loginUrl);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
$result=curl_exec($ch);
curl_close($ch);
var_dump(json_decode($result));

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

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