PHP Classes

PHP Exchange Rate API: Get exchange rate for currency and precious metals

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
StarStarStarStar 66%Total: 770 All time: 4,395 This week: 57Up
Version License PHP version Categories
currencylayer 1.0GNU General Publi...5PHP 5, Web services, Finances
Description 

Author

This class can get exchange rate for currency and precious metals.

It can send HTTP requests to currencylayer Web service API Web server to request the exchange rates between currencies and precious metals.


Currently it supports 168 currencies and precious metals, including Bitcoin supported.

It also provides historical data as far back as the year 2000.

Innovation Award
PHP Programming Innovation award nominee
January 2016
Number 14
Getting the exchange rate is useful often in applications that deal with multiple currencies.

However, sometimes you need not only the rates for exchanging between currencies on the current day, but also on past dates.

This class provides means to obtain currency and precious metal exchange rates on any past date starting the year 2000 using the currencylayer API.

Manuel Lemos
Picture of Dave Smith
  Performance   Level  
Name: Dave Smith is available for providing paid consulting. Contact Dave Smith .
Classes: 51 packages by
Country: United States United States
Age: 59
All time rank: 608 in United States United States
Week rank: 48 Up7 in United States United States Up
Innovation award
Innovation award
Nominee: 32x

Winner: 7x

Example

<?php
/*
example usage
currencyLayer ver 1.0

You must get an API key from https://currencylayer.com/product
and enter it in the currency.class.php file
*/

//turning off low level notices
error_reporting(E_ALL ^ E_NOTICE);

//set your plan to diplay features associated with it.
//plans are free, basic, professional and enterprise
$plan = 'free';

//instantiate the class
include('currency.class.php');
$currencyLayer = new currencyLayer();

/*
Get basic quotes
*/

//set our endpoint
//defaults to the live endpoint, but we will set it just to be safe
$currencyLayer->setEndPoint('live');

//specify the currencies we want quotes for
//if this parameter is not set, quotes for all 168 supported currencies will be returned
//we want to limit the bandwidth and response time by only getting the currencies we need
$currencyLayer->setParam('currencies','BTC,EUR,GBP');

//get the response from the api
$currencyLayer->getResponse();

//the reponse property will contain the response returned from the api
echo '<h4>Basic quote request for Bitcoin, Euro and British Pound</h4>';
echo
'USD to BTC = '.$currencyLayer->response->quotes->USDBTC.'<br>';
echo
'USD to EUR = '.$currencyLayer->response->quotes->USDEUR.'<br>';
echo
'USD to GBP = '.$currencyLayer->response->quotes->USDGBP.'<br>';

/*
Convert currency using the class method
*/

echo '<h4>Currency conversion using class method</h4>';
echo
'10 USD = '.$currencyLayer->convertCurrency(10,'BTC').' BTC<br>';
echo
'10 USD = '.$currencyLayer->convertCurrency(10,'EUR').' EUR<br>';
echo
'10 USD = '.$currencyLayer->convertCurrency(10,'GBP').' GBP<br>';

/*
Get historical quotes
*/

//we still have our previous parameters set, so we don't need to set them again
//we do need to change to the historical endpoint
$currencyLayer->setEndPoint('historical');

//specify the date we want quotes from
//date format is YYYY-MM-DD
$currencyLayer->setParam('date','2010-04-15');

//get the response from the api
$currencyLayer->getResponse();

//the response property now contains the new response
echo '<h4>Historical quote request</h4>';
echo
'Exchange rate on '.$currencyLayer->response->date.'<br>';
echo
'USD to BTC = '.$currencyLayer->response->quotes->USDBTC.'<br>';
echo
'USD to EUR = '.$currencyLayer->response->quotes->USDEUR.'<br>';
echo
'USD to GBP = '.$currencyLayer->response->quotes->USDGBP.'<br>';




if(
$plan == 'basic' OR $plan == 'professional' OR $plan == 'enterprise' ){
   
/*
Works in all plans except free
*/
   
/*
Switch source currency
*/
   
    //make sure our endpoint is live
   
$currencyLayer->setEndPoint('live');
   
   
//set our source currency to the Australian dollar
   
$currencyLayer->setParam('source','AUD');
   
   
//we have previously set the date parameter
    //remove the date parameter that we don't want with this request
   
$currencyLayer->setParam('date',null);
   
   
//get the response from the api
   
$currencyLayer->getResponse();
   
   
//we now have quotes relative to the AUD
   
echo '<h4>Source currency changed to the AUD</h4>';
    echo
'AUD to BTC = '.$currencyLayer->response->quotes->AUDBTC.'<br>';
    echo
'AUD to EUR = '.$currencyLayer->response->quotes->AUDEUR.'<br>';
    echo
'AUD to GBP = '.$currencyLayer->response->quotes->AUDGBP.'<br>';
   
    if(
$plan="professional" OR $plan = 'enterprise' ){
       
/*
Works in the professional and enterprise plans
*/
   
/*
currency conversion through the api
*/
       
        //reset all our parameters and start over
       
$currencyLayer->resetParams();
       
       
//set our endpoint to convert
       
$currencyLayer->setEndPoint('convert');
       
       
//convert from AUD
       
$currencyLayer->setParam('from','AUD');
       
       
//convert to USD
       
$currencyLayer->setParam('to','USD');
       
       
//set amount of AUD to convert
       
$currencyLayer->setParam('amount',10);
       
       
//get the response from the api
       
$currencyLayer->getResponse();
       
        echo
'<h4>Convert Currency through API</h4>';
        echo
$currencyLayer->response->query->amount.' '.$currencyLayer->response->query->from.' = '.$currencyLayer->response->result.' '.$currencyLayer->response->query->to.'<br>';
       
    }
   
}

?>


  Files folder image Files (3)  
File Role Description
Plain text file currency.class.php Class Main Class
Accessible without login Plain text file example.php Example Example Usage
Accessible without login Plain text file license.txt Lic. License

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 0%
Total:770
This week:0
All time:4,395
This week:57Up
 User Ratings  
 
 All time
Utility:100%StarStarStarStarStarStar
Consistency:83%StarStarStarStarStar
Documentation:-
Examples:83%StarStarStarStarStar
Tests:-
Videos:-
Overall:66%StarStarStarStar
Rank:486