从URL获取JSON对象 [英] Get JSON object from URL
本文介绍了从URL获取JSON对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个返回如下JSON对象的URL:
I have a URL that returns a JSON object like this:
{
"expires_in":5180976,
"access_token":"AQXzQgKTpTSjs-qiBh30aMgm3_Kb53oIf-VA733BpAogVE5jpz3jujU65WJ1XXSvVm1xr2LslGLLCWTNV5Kd_8J1YUx26axkt1E-vsOdvUAgMFH1VJwtclAXdaxRxk5UtmCWeISB6rx6NtvDt7yohnaarpBJjHWMsWYtpNn6nD87n0syud0"
}
我想获取access_token
值.那么如何通过PHP检索它呢?
I want to get the access_token
value. So how can I retrieve it through PHP?
推荐答案
$json = file_get_contents('url_here');
$obj = json_decode($json);
echo $obj->access_token;
为此,file_get_contents
要求启用allow_url_fopen
.可以在运行时完成以下操作:
For this to work, file_get_contents
requires that allow_url_fopen
is enabled. This can be done at runtime by including:
ini_set("allow_url_fopen", 1);
您还可以使用 curl
来获取网址.要使用curl,您可以使用在此处找到的示例:
You can also use curl
to get the url. To use curl, you can use the example found here:
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, 'url_here');
$result = curl_exec($ch);
curl_close($ch);
$obj = json_decode($result);
echo $obj->access_token;
这篇关于从URL获取JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文