PHP code examples
The following code examples demonstrate the PHP interaction with the API.
All examples also expect your
API key.
Example 1
Get the exchange rate between two currencies
$data = file_get_contents(
'http://jsonrates.com/get/?'.
'from=USD'.
'&to=EUR'
);
$json = json_decode($data);
$rate = (float) $json->rate;
Example 2
Convert an amount from one into another currency
$data = file_get_contents(
'http://jsonrates.com/convert/?'.
'from=USD'.
'&to=EUR'.
'&amount=2.99'
);
$json = json_decode($data);
$amount = (float) $json->amount;
Example 3
Get historical exchange rates between two currencies for three days
$data = file_get_contents(
'http://jsonrates.com/historical/?'.
'from=USD'.
'&to=EUR'.
'&dateStart=2014-06-25'.
'&dateEnd=2014-06-27'
);
$json = json_decode($data);
foreach ($json->rates as $date => $values) {
$histrate = (float) $values->rate;
}