问题描述
我正在使用currencylayer JSON API获取实时货币转换值-有人知道我如何使用PHP从下面的API响应中同时获取"result"
值和"quote"
值吗?
I am using currencylayer JSON API to get realtime currency conversion value - does anybody know how I could grab both the "result"
value and the "quote"
value from the API response below using PHP?
我是PHP的新手,我想知道是否可以将其存储在变量中.
I am new to PHP, and I am wondering if it's possible to store it in a variable.
这是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
}
我玩过file_get_contents("URL")
,但是我不明白如何获取单个值.
I have played around with file_get_contents("URL")
but I didnt understand how to get a single value out.
请求网址如下:
https://apilayer.net/api/convert?access_key=...&from=CAD&to=GBP&amount=234
感谢您的帮助!
推荐答案
好吧,可以说json响应位于名为$response
的变量中,您必须使用json_decode
然后执行以下操作:
Ok, lets say that json response is in a variable named $response
, you must use json_decode
and then do as follows:
$decoded = json_decode($response);
$result = $decoded->result;
$quote = $decoded->info->quote;
var_dump($result, $quote);
这篇关于PHP从API响应中获取json汇率值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!