Code Sample: Convert one Currency to another

As already mentioned, format in which currency exchange rates are returned by most providers is JSON. If you know how, it's pretty easy to parse using e.g. PHP cURL or jQuery.ajax. We'll give you an idea about how you can get up and running converting any values from one currency to another.

In order to keep this as simple as possible, this code example is also based on the currencylayer API:


API Request URL

Unlike the usual API request URL, this one contains a few more parameters - from, to and amount. The function of each should be rather self-explaining. This is how it looks like (in our case):



https://apilayer.net/api/convert
    ? access_key = YOUR-PERSONAL-API-KEY
    & from = USD
    & to = GBP       
    & amount = 10   

Remember this URL, since we are going to need it in the language examples below. (referred to as "YOUR_REQUEST_URL")


Don't have an API Access Key yet? Get one here.



JSON API Response

Just like when requesting real-time rates, the API will answer with a simple JSON response, again containing different objects for each information. Most likely the most useful object to you will be result, as it contains your conversion value. Now, how can we parse this thing? See below!



{
  "terms": "https://currencylayer.com/terms",
  "privacy": "https://currencylayer.com/privacy",
  "query":{
    "from": "USD",
    "to": "GBP",
    "amount": 10
  },
  "info":{
    "timestamp": 1433345648,
    "quote": 0.651897
  },
  "result": 6.51897
}


Parsing the JSON API Response

Again, we'll give you examples in PHP (cURL) and AJAX (jQuery), since most of you are going to be using these. (we'd like to keep this as simple as possible)


PHP cURL

You can simply access your conversion result in PHP cURL, like so:



// Initialize cURL:
$endpoint = 'convert';
$access_key = 'YOUR-PERSONAL-API-KEY';

$from = 'USD';
$to = 'EUR';
$amount = 10;

// initialize CURL:
$ch = curl_init('https://apilayer.net/api/'.$endpoint.'?access_key='.$access_key.'&from='.$from.'&to='.$to.'&amount='.$amount.'');   
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// get the (still encoded) JSON data:
$json = curl_exec($ch);
curl_close($ch);

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

// access the conversion result
echo $conversionResult['result'];

Simple, right? Your conversion result is stored in $conversionResult['result'].


AJAX (jQuery)

Same thing can be done with jQuery.ajax.



// set endpoint and your access key
endpoint = 'convert';
access_key = 'YOUR-PERSONAL-API-KEY';

// define from currency, to currency, and amount
from = 'EUR';
to = 'GBP';
amount = '10';

// Initialize AJAX and perform conversion
$.ajax({
    url: 'https://apilayer.net/api/' + 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);
                
    }
});

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

Source: https://currencylayer.com/documentation