Get latest rates (JSON)
Get the latest possible rates (minimum age 5 min)
Print all rates line by line
Get all the rates with base currency Euro. Every other base is also possible. How to alter the url: URL scheme
Here is how to get the rates and print them to console.log()
using Nodejs and the request library.
const request = require('request');
//Make request
request('https://api.currex.info/json/latest/EUR', function (error, response, body) {
//Error catching
if (error){
console.error(error);
return;
}
if(response.statusCode != 200) {
console.error("Request Response Status Code: "+ response.statusCode);
}
//Parse JSON data
let responseObj = JSON.parse(body);
//Dataset timestamp
//*1000 because JavaScript uses milliseconds for date - unix timestamp is only seconds
let dataTimestamp = new Date(responseObj.timestamp*1000);
console.info("Data Timestamp: "+ dataTimestamp);
//Iterate over rates and print them
//foreach-loop for objects - kind of ;)
Object.keys(responseObj.rates).forEach(function(key) {
let singleRate = responseObj.rates[key];
//*1000 because JavaScript uses milliseconds for date - unix timestamp is only seconds
let singleRateTimestamp = new Date(singleRate.timestamp*1000);
let singleRateRate = singleRate.rate;
let singleRateCurrencyCode = singleRate.currency;
console.log("["+singleRateTimestamp+"]: 1 EUR = "+singleRateRate+" "+singleRateCurrencyCode);
});
});
Here is how to get the rates and print them using PHP and file_get_contents()
.
$apiURL = "https://api.currex.info/json/latest/EUR/";
$jsonApiData = file_get_contents($apiURL);
//Decode the the json to php object
$apiObj = json_decode($jsonApiData);
//dataset timestamp
printf(date('c',$apiObj->timestamp).'<br>');
//Iterate trough object
foreach($apiObj->rates as &$rate){
printf("[".date('c',$rate->timestamp)."]: 1 EUR = ".$rate->rate." ".$rate->currency."<br>");
}