我正在使用currencylayer JSON API来获取实时货币转换价值 - 有人知道如何使用PHP从下面的API响应中获取"result"值和"quote"值吗?
我是PHP的新手,我想知道是否可以将它存储在变量中.
这是JSON:
{
"success":true,
"terms":"https:\/\/currencylayer.com\/terms",
"privacy":"https:\/\/currencylayer.com\/privacy",
"query":{
"from":"CAD",
"to":"GBP",
"amount":234
},
"info":{
"timestamp":1432829168,
"quote":0.524191
},
"result":122.660694
}
Run Code Online (Sandbox Code Playgroud)
我玩过,file_get_contents("URL")但我不明白如何获得单一价值.
请求网址如下所示:
https://apilayer.net/api/convert?access_key=...&from=CAD&to=GBP&amount=234
谢谢您的帮助!
好吧,让我们说json响应是在一个名为的变量中$response,你必须使用json_decode然后执行如下操作:
$decoded = json_decode($response);
$result = $decoded->result;
$quote = $decoded->info->quote;
var_dump($result, $quote);
Run Code Online (Sandbox Code Playgroud)