Sample Code
PHP (cURL)
Real-Time Rates
Find below a simple PHP example for getting exchange rate data via the Exchangerates API's latest endpoint.
// set API Endpoint and API key
$endpoint = 'latest';
$access_key = 'API_KEY';
// Initialize CURL:
$ch = curl_init('https://api.exchangeratesapi.io/v1/'.$endpoint.'?access_key='.$access_key.'');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Store the data:
$json = curl_exec($ch);
curl_close($ch);
// Decode JSON response:
$exchangeRates = json_decode($json, true);
// Access the exchange rate values, e.g. GBP:
echo $exchangeRates['rates']['GBP'];Convert Currencies
Here is how to convert currencies using PHP cURL.
// set API Endpoint, access key, required parameters
$endpoint = 'convert';
$access_key = 'API_KEY';
$from = 'USD';
$to = 'EUR';
$amount = 10;
// initialize CURL:
$ch = curl_init('https://api.exchangeratesapi.io/v1/'.$endpoint.'?access_key='.$access_key.'&from='.$from.'&to='.$to.'&amount='.$amount.'');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// get the JSON data:
$json = curl_exec($ch);
curl_close($ch);
// Decode JSON response:
$conversionResult = json_decode($json, true);
// access the conversion result
echo $conversionResult['result'];JavaScript (jQuery.ajax)
Real-Time Rates
Find below a simple jQuery.ajax example for getting exchange rate data via the Exchangerates API's latest endpoint.
// set endpoint and your access key
endpoint = 'latest'
access_key = 'API_KEY';
// get the most recent exchange rates via the "latest" endpoint:
$.ajax({
url: 'https://api.exchangeratesapi.io/v1/' + endpoint + '?access_key=' + access_key,
dataType: 'jsonp',
success: function(json) {
// exchange rata data is stored in json.rates
alert(json.rates.GBP);
// base currency is stored in json.base
alert(json.base);
// timestamp can be accessed in json.timestamp
alert(json.timestamp);
}
});Convert Currencies
Here is how to convert currencies using jQuery.aja.
// set endpoint and your API key
endpoint = 'convert';
access_key = 'API_KEY';
// define from currency, to currency, and amount
from = 'EUR';
to = 'GBP';
amount = '10';
// execute the conversion using the "convert" endpoint:
$.ajax({
url: 'https://api.exchangeratesapi.io/v1/' + endpoint + '?access_key=' + access_key +'&from=' + from + '&to=' + to + '&amount=' + amount,
dataType: 'jsonp',
success: function(json) {
// access the conversion result in json.result
alert(json.result);
}
});