Free API To Convert Currency – Implemented In PHP

Free API To Convert Currency is a tutorial on how to get a free api to convert from one currency to another. In this tutorial, we show how to do this currency conversion using PHP, but this does not mean that it cannot be implemented in other programming languages.

Get The Free Currency Conversion API

You need to go to to obtain the free currency converter api.

Please note that this guys have paid api services as you will see when you get there, so follow the arrow markings on the diagrams below to get the free api.

Copy the free API key from the email address you supplied and do not forget to click on the link to verify you email after copying your api key.

Also copy the url provided in the mail which has your api key attached to it.

Currency Converter Class

Let’s write the currency converter class using the api key and the url obtained from the mail. Our class will have two methods convertCurrency() and Execute_Transaction().

One thing you should do right now is to find out if curl extension is enabled in your PHP configuration on your web server. Don’t run this code on your localhost because in my own experience the code fails to convert the currency on the localhost for some reason that i am yet to investigate.

Login to your cpanel and follow the markings on the images below to know if curl is enabled on your web server:

See currency converter class code:

class CurrencyConverter {
    public static function convertCurrency() {
        $url = "";
        $curl = curl_init(); //Initialiaze curl
        if ($curl === false) {
            throw new Exception('failed to initialize');

        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_POST, 0);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
        return $curl;
    public static function Execute_Transaction($curl) {
        $response = curl_exec($curl);
        return $response;

$url in the code above is the url you copied from your email and is assigned the free currency converter API key which was copied also from your email.

Notice the variable q after ? appended on the query string. This variable q is assigned USD_NGN. This are the currencies i am converting, I am converting from US dollars to Nigerian Naira in this instance; you will have to change this to suite what currencies you are converting.

Use the url below to see list of currencies:

If you have not used curl in php before now, go here to quickly learn how to use curl in PHP so that this post can focus on its subject.

Reference The Currency Converter Class

On the page where we are to use the currency converter class, we first of all reference our class thus:

include ('classes/CurrencyConverter.php');

Convert The Currency

//Call the class method to convert the currency
$curl = CurrencyConverter::convertCurrency();
if ($curl === false) {
     //handle the error message here
//Call the Execute_Transaction of the CurrencyConverter class here
$response = CurrencyConverter::Execute_Transaction($curl);
if ($response === false) {
     //handle the error message here
//decode the json response returned from executing the curl object here
$responseArry = json_decode($response);
$convertedUSD = $responseArry->USD_NGN;

//do some more processing according to my needs
$dollarEquivalentOfNGN = 6000 / $convertedUSD;
$dollarEquivalentOfNGN = number_format($dollarEquivalentOfNGN, 2);

//close curl object here


$convertedUSD holds the conversion of 1 US dollars to naira. In my case, i needed to know what NGN6000 will convert to daily in US dollars so that people who want to pay in US dollars pay nothing short of NGN6000 at all times. holds the conversion of 1 US dollars (USD) to naira (NGN). In my case, i needed to know what NGN6000 will convert to daily in US dollars so that people who want to pay in US dollars pay nothing short of NGN6000 at all times.

Once i know what 1 USD converts to in Naira which is stored in $convertedUSD, i then divide NGN6000 by that $convertedUSD to get the current dollar equivalent.

The currency conversion API owners say that the free version of the currency conversion api currencies database gets updated once a day while the paid version get updated every hour. I think you should have this information; for me i am okay with updating at least once a day even though i really think that based on my observation, the dollar equivalent of NGN6000 changes more than once a day on the site where i use it, you can check it here

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

You May Also Like