PHP Classes

Free PHP Currency Conversion API Script with Source Code to Get Forex Exchange Rates - PHP Exchange Rate API package blog

Recommend this page to a friend!
  All package blogs All package blogs   PHP Exchange Rate API PHP Exchange Rate API   Blog PHP Exchange Rate API package blog   RSS 1.0 feed RSS 2.0 feed   Blog Free PHP Currency Con...  
  Post a comment Post a comment   See comments See comments (21)   Trackbacks (0)  


Viewers: 4,781

Last month viewers: 478

Package: PHP Exchange Rate API

Foreign exchange rates for currency are constantly fluctuating from one minute to the next. So those Euro's you have may be worth more, or even less US Dollars throughout the day.

Using the PHP Exchange Rate API we can access real time and historical rates through the currencyLayer API.

Read this article to learn how to retrieve currency exchange rates for many currencies now, as well in paste dates up to the year 2000.

Loaded Article


PHP Currency Converter API

PHP Currency Converter API

The PHP Exchange Rate API package uses data provided by the currencyLayer service API. This is an API that provides currency information which is updated every 60 minutes for the free plan and up to every 60 seconds in the premium plans.

I like getting things for free, so this article will mostly focus on what is available to us with the free plan. I will also show you how to use the free plan to perform premium features like source switching and currency conversion. At the end of the article I will briefly discuss the other premium features of Time Frame Queries and Currency Change Queries.

If you will be following along with the code using the currency class, you will need your own access key which is available by signing up at Make sure you change the $apiKey property in the currencyLayer class to your unique key.

The first thing we need to do is include the currencyLayer class and instantiate it.

error_reporting(E_ALL ^ E_NOTICE);
$currencyLayer = new currencyLayer();

The currencyLayer API has 5 different endpoints, of which we are mostly interested in the 'live' and 'historical' endpoints. For this example, we want live exchange rates so we will use the setEndpoint method on the next line of our code to specify the live endpoint.


The API supports 168 world currencies, for a full list of the codes and currencies you can visit

If we where to send our request now, all 168 currencies would be returned. We don't need them all, so we will limit the results to Bitcoins, Euro's and British Pounds by adding the following line...


The parameter 'currencies' is supported by the API to limit currencies returned which will save you time and bandwidth by using it.

Notice that we are using the setParam method which specifies the parameter and value, which is a comma separated list of currency codes. The currency codes can be found by following the link for supported currencies provided above.

We are ready to send our request to the API and get back their response by using the getResponse method.


We can check to see if there was an error by checking the error property...

if( !empty($currencyLayer -> error -> code) ) {
   //handle the error
   echo 'The following error was reported - ' . $currencyLayer -> error -> text;

If the request was successful, then the response will contain the conversion rates to US Dollars from our requested currencies. The code for these rates are our source currency (USD) and conversion currency. So the code from USD to Euro's would be 'USDEUR' which we can display using the following line:

echo 'USD to EUR = '. $currencyLayer -> response -> quotes -> USDEUR;

You may have noticed that we did not specify that our source currency was US Dollars. This is the default source currency used by currencyLayer and can only be changed if you have a premium plan.

If you purchased a premium plan you can easily change the source currency using the 'source' parameter, if you are using the free plan I will show you how to do a secondary conversion shortly.

Suppose we want the conversion rates from Euro's to Bitcoins and British Pounds.

Using a premium plan we can easily set up our request with the following lines:

$currencyLayer -> setParam( 'source', 'EUR');
$currencyLayer -> setParam( 'currencies', 'BTC,GBP');
$currencyLayer -> getResponse();

The response quotes would then be found in:

$currencyLayer -> response -> quotes -> EURBTC
$currencyLayer -> response -> quotes -> EURGBP

If we only have the free plan, then this will not be much use to us. We will receive an error that our plan does not support this feature. To get the results we are after, we will have to perform a secondary conversion based on conversion rates for the US Dollar. Don't worry, it is easier than it sounds.

$currencyLayer -> setParam( 'currencies', 'BTC,EUR,GBP');
$currencyLayer -> getResponse();
$EURBTC = $currencyLayer -> response -> quotes -> USDBTC / $currencyLayer -> response -> quotes -> USDEUR;
$EURGBP = $currencyLayer -> response -> quotes -> USDGBP / $currencyLayer -> response -> quotes -> USDEUR;

What we have done is gotten the conversion rates based on the default US Dollars and used those to figure the conversion rates for Euro's, which will be the values of our $EURBTC and $EURGBP variables.

The currencyLayer API provides a 'convert' endpoint to calculate the conversion from one currency to another. This endpoint is only available in premium plans, however the class also provides a method for this conversion which can be used by us frugal users for free.

First we will look at conversion through the API for premium users:

$currencyLayer -> setEndPoint( 'convert' );
$currencyLayer -> setParam( 'from', 'USD' );
$currencyLayer -> setParam( 'to', 'AUD');
$currencyLayer -> setParam( 'amount', 10);
$currencyLayer -> getResponse();

This will convert 10 US Dollars to Australian Dollars which we can access through the following property values, the result will contain the conversion value.

$currencyLayer -> response -> query -> amount
$currencyLayer -> response -> query -> from
$currencyLayer -> response -> query -> to
$currencyLayer -> response -> result

To do this using the class, we would use the following code:

$currencyLayer -> setEndPoint( 'live' );
$currencyLayer -> setParam( 'currencies', 'AUD');
$currencyLayer -> getResponse();
$result = $currencyLayer -> convertCurrency( 10, 'AUD');

We are using the convertCurrency method with the amount and currency to convert to which will return our conversion value.

We can use the 'historical' endpoint to get the conversion rate at the close of the market for any supported currency as far back as the year 2000.

This endpoint works exactly like the live endpoint except that we also have to supply the date we want the conversion rate for. Our code would look something like this...

$currencyLayer -> setEndPoint( 'historical' );
$currencyLayer -> setParam( 'date', '2010-04-15' );
$currencyLayer -> setParam( 'currencies', 'EUR,GBP' );
$currencyLayer -> getResponse();

Note that the value for the 'date' parameter must be formatted as YYYY-MM-DD. In this example we are getting the conversion rates for Euro's and British Pounds on April 15th of 2010.

We would access the quotes exactly like we did with the live data...

$currencyLayer -> response -> quotes -> USDEUR

The 'timeframe' endpoint is available to Professional and Enterprise premium plan users. This endpoint allows you to set a start_date and end_date which will return conversion rates for your specified currencies on each day between the start and end up to a maximum of 365 days.

You would access the quotes like this:

$currencyLayer -> response -> quotes -> 2010-04-15 -> USDEUR
$currencyLayer -> response -> quotes -> 2010-04-16 -> USDEUR

The 'change' endpoint is only available to Enterprise premium plan users. This endpoint will return the margin and percent change from a specified start_date (or the end of the previous day if not specified) and the most current conversion rate for one or more currencies.

You would access the returned values like this:

$currencyLayer -> response -> quotes -> USDEUR -> start_rate
$currencyLayer -> response -> quotes -> USDEUR -> end_rate
$currencyLayer -> response -> quotes -> USDEUR -> change
$currencyLayer -> response -> quotes -> USDEUR -> change_pct

As you can see, we can do quite a bit with our free access to the currencyLayer services since we can get both live and historical data. If you need more than 1,000 API accesses in a month then you will need to select the premium plan that best suits your needs.

If you want the quotes updated every 10 minutes then you will need the professional plan or updated every 60 seconds will require the enterprise plan.

While free is always nice, it is also important to remember that our clients or businesses that use the service extensively support our casual free use through premium plans.

If you like this article share with other developers that may want to know about a good currency conversion solution. If you have questions post a comment below.

You need to be a registered user or login to post a comment

Login Immediately with your account on:


19. where is the download? - bickern (2022-04-18 08:16)
where is the download?... - 0 replies
Read the whole comment and replies

18. Nice - Manuel (2021-05-31 09:16)
Big up... - 0 replies
Read the whole comment and replies

17. Goods - elias (2021-02-09 01:45)
Gooods... - 0 replies
Read the whole comment and replies

16. Mal Mal wikunumemu - ?????? ???? ??????? (2019-05-14 06:25)
kk... - 0 replies
Read the whole comment and replies

15. Live Exchange Currency - cappssolution (2019-02-12 09:17)
Live exchange... - 0 replies
Read the whole comment and replies

14. Excellent Source of anything related to Web. - cappssolution (2019-02-05 14:19)
PHP... - 0 replies
Read the whole comment and replies

13. Great API - Sanjay (2018-06-25 08:52)
Great API... - 0 replies
Read the whole comment and replies

12. Api - step click (2018-03-18 18:14)
-... - 0 replies
Read the whole comment and replies

11. ghfgh - hkimo2003 (2017-12-11 09:36)
gn... - 0 replies
Read the whole comment and replies

10. afa - vadivel (2017-11-06 04:46)
asdfa... - 0 replies
Read the whole comment and replies

  Post a comment Post a comment   See comments See comments (21)   Trackbacks (0)  
  All package blogs All package blogs   PHP Exchange Rate API PHP Exchange Rate API   Blog PHP Exchange Rate API package blog   RSS 1.0 feed RSS 2.0 feed   Blog Free PHP Currency Con...