PHP grab json exchange rate value from API response
I am using javascript API for jQuery to get realtime currency conversion value. Does anyone know how I can get the value "result"
and value "quote"
from the API response below using PHP?
I'm new to PHP and I'm wondering if it is possible to store it in a variable.
This is 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
}
I played with file_get_contents("URL")
, but I didn't understand how to get one value.
The request url looks like this:
https://apilayer.net/api/convert?access_key=...&from=CAD&to=GBP&amount=234
Thanks for the help!
Ok, let's say the json response is in a variable named $response
, you have to use json_decode
and then do this:
$decoded = json_decode($response); $result = $decoded->result; $quote = $decoded->info->quote; var_dump($result, $quote);
try it
$jsonArray = file_get_contents($yourUrl);
$jsonObject = json_decode($jsonArray);
echo $jsonObject->result;
echo $jsonObject->info->quote;