Code Sample: Get Live Exchange Rates

The five currency converter APIs we compared deliver exchange rates in three different formats: JSON, CSV and XML. In order to keep this as simple as possible, we'll choose the format three out of five have in common (JSON) and outline the most important steps of parsing such API responses using different programming languages and integrating the data in an application of your own.

The following code and samples are based on one of our favorite API services, the currencylayer API:


API Request URL

First of all, you need to get familiar with how the structure of the respective API's request URL looks and works - in our example (using the currencylayer API) it looks like this:



https://apilayer.net/api/live
    ? access_key = YOUR-PERSONAL-API-KEY
    & currencies = EUR,GBP,CAD,PLN
    & source = USD

See how we defined a few important parameters? source, in this case, refers to the currency to which all other currencies are relative to. (also known as base currency) The other two should be self-explaining.


Not sure about the access_key parameter? Get your free API Access Key here.



JSON API Response

Below you can see the typical JSON API response we would get after executing the above query, containing a number of objects and sub-objects. The timestamp object, for example, contains a UNIX time stamp which indicates the exact time the currency exchange rates (which are located in the quotes object) were collected.



{
  "terms": "https://currencylayer.com/terms",
  "privacy": "https://currencylayer.com/privacy",
    "timestamp": 1430401802,
    "source": "USD",
    "quotes": {
       "USDEUR":0.887591,
       "USDGBP":0.652273,
       "USDCAD":1.244725,
       "USDPLN":3.68835
    [...]
    }
}  


Parsing the JSON API Response

Now, there are many ways you can make use of this data, we'll give you some examples for PHP (cURL) and AJAX (jQuery), since most of you are going to be using these. (again, we'd like to keep this simple)


PHP cURL

In case you are wondering why we are using PHP cURL and not a simpler approach like the PHP file_get_contents() function - to quote a Stack Overflow answer:

"file_get_contents() is a simple screwdriver. Great for simple GET requests where the header, HTTP request method, timeout, cookiejar, redirects, and other important things do not matter. cURL with setopt are powerdrills with every bit and option you can think of."



// Initialize cURL:
$ch = curl_init($YOUR_REQUEST_URL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Store the data:
$json = curl_exec($ch);
curl_close($ch);

// Decode JSON response:
$rates = json_decode($json, true);

// Access the exchange rate values, e.g. GBP:
echo $rates['quotes']['USDGBP'];            

See how easy this is? You can simply access a certain exchange rate like in a usual PHP array - using $rates['quotes'][$YOUR_PREFERRED_CURRENCY_PAIR]


AJAX (jQuery)

Now, the same can be done with jQuery.ajax:



// Initialize AJAX
$.ajax({
    url: YOUR_REQUEST_URL,   
    success: function(json) {

        // exchange rata data is stored in json.quotes
        alert(json.quotes.USDGBP);

    }
});

Actually, this is even easier. Just access each currency exchange rate value using alert(json.quotes.YOUR_PREFERRED_CURRENCY_PAIR)


Thanks for reading! Please note that we do not own any of the above content.

Source: https://currencylayer.com/documentation