我有一个返回JSON对象的URL,如下所示:
{
"expires_in":5180976,
"access_token":"AQXzQgKTpTSjs-qiBh30aMgm3_Kb53oIf-VA733BpAogVE5jpz3jujU65WJ1XXSvVm1xr2LslGLLCWTNV5Kd_8J1YUx26axkt1E-vsOdvUAgMFH1VJwtclAXdaxRxk5UtmCWeISB6rx6NtvDt7yohnaarpBJjHWMsWYtpNn6nD87n0syud0"
}
我想从URL获取JSON对象,然后获取access_token
值。
那么如何通过PHP检索它呢?
您可以使用PHP的json_解码函数:
$url = "http://urlToYourJsonFile.com";
$json = file_get_contents($url);
$json_data = json_decode($json, true);
echo "My token: ". $json_data["access_token"];
$url = 'http://.../.../yoururl/...';
$obj = json_decode(file_get_contents($url), true);
echo $obj['access_token'];
Php还可以将属性与破折号一起使用:
garex@ustimenko ~/src/ekapusta/deploy $ psysh
Psy Shell v0.4.4 (PHP 5.5.3-1ubuntu2.6 — cli) by Justin Hileman
>>> $q = new stdClass;
=> <stdClass #000000005f2b81c80000000076756fef> {}
>>> $q->{'qwert-y'} = 123
=> 123
>>> var_dump($q);
class stdClass#174 (1) {
public $qwert-y =>
int(123)
}
=> null
$json = file_get_contents('url_here');
$obj = json_decode($json);
echo $obj->access_token;
要做到这一点,file_get_contents
需要启用allow_url_fopen
。这可以在运行时通过包括:
ini_set("allow_url_fopen", 1);
您还可以使用curl
获取url。要使用curl,可以使用以下示例:
$ch = curl_init();
// IMPORTANT: the below line is a security risk, read https://paragonie.com/blog/2017/10/certainty-automated-cacert-pem-management-for-php-software
// in most cases, you should set it to true
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;
问题内容: 我有一个网址,返回的是这样的JSON对象: 我想获得价值。那么如何通过PHP检索它呢? 问题答案: $json = file_get_contents(‘url_here’); $obj = json_decode($json); echo $obj->access_token; 为此,需要启用它。可以通过在运行时完成以下操作来实现: 您也可以使用获取网址。要使用卷曲,可以使用中发现的
我需要用函数SSJS from mJson()读一个URL。例如Notes View的数据访问API http://{host}/{database}/api/data/collections/name/{name} 我该怎么做? P. S我认为(我不知道是否是真的),如果我使用Java代码(例如类URLReader从这个博客,我失去作者/读者的功能,因为是我的服务器,而不是当前用户执行读取流?
我有一个名为AnimalsData的文件,它有一个由数组组成的对象。数组由一个对象和另一个数组组成。它看起来像这样: 我的HTML文件如下所示: 我正试图获取的值的类名对象从动画类数组。我一直在使用以下两个stackoverflow页面作为指南: 访问JSON数组中的对象和访问/处理(嵌套)对象、数组或JSON 我有另一个javascript文件,其中包含以下内容: 编辑我将for in循环中的"
我们的中央银行以多种方式提供货币汇率。例如,一个货币日期很容易获得:http://api.nbp.pl/api/exchangerates/rates/a/usd/2020-08-20?format=Json(它以简单的大括号开始{作为典型的Json) 但是另一个表-每个货币日期:http://api.nbp.pl/api/exchangerates/tables/a/2020-08-20?for
你好,我来自从URL获取JSON对象 我试着去买一顶帽子- 有人能告诉我我做错了什么吗?