PHP Classes

PHP Get Currency Rates with Fixer API: Convert currency amounts at present and past times

Recommend this page to a friend!
  Info   View files Example   View files View files (3)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 99 This week: 1All time: 9,797 This week: 560Up
Version License PHP version Categories
fixer 1.0.0GNU General Publi...5.6PHP 5, Web services, Finances
Description 

Author

This package can convert currency amounts at present and past times.

It can retrieve the current or past rates of conversion between two currencies by sending HTTP requests to the Fixer.io API Web service server.

The package can take an amount for a given currency and returns the converted value to another currency.

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: 58
All time rank: 618 in United States United States
Week rank: 21 Up4 in United States United States Up
Innovation award
Innovation award
Nominee: 32x

Winner: 7x

Example

<?php
/*
example usage
fixerWrapper ver 1.0

You must get an API key from https://fixer.io/product
and enter it in the fixer.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 pro-plus
$plan = 'free';

//instantiate the class
include('fixer.class.php');
$fixer = new fixerWrapper();

/*
Get current rates
*/

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

//specify the currencies by symbols that we want rates for
//if this parameter is not set, rates for all 170 supported currencies will be returned
//we want to limit the bandwidth and response time by only getting the currencies we need
$fixer->setParam('symbols','USD,GBP,BTC');

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

//the reponse property will contain the response returned from the api
//note that the default base currency is EUR (euro)
echo '<h4>Basic rate request for US Dollar, British Pound and Bitcoin</h4>';
echo
'EUR to USD = '.$fixer->response->rates->USD.'<br>';
echo
'EUR to GBP = '.$fixer->response->rates->GBP.'<br>';
echo
'EUR to BTC = '.$fixer->response->rates->BTC.'<br>';

/*
Convert currency using the class method
this method does not make a request to the api
*/

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

/*
Change base using the class method
Note that the base can be changed to any currency which is available in the current response
the current base will then be available as a symbol in the new response
this method does not make a request to the api
*/

echo '<h4>Changing the currency base using class method to USD</h4>';
$fixer->changeBase('USD');
echo
'10 USD = '.$fixer->convertTo(10,'EUR').' EUR<br>';
echo
'10 USD = '.$fixer->convertTo(10,'GBP').' GBP<br>';
echo
'1000 USD = '.$fixer->convertTo(1000,'BTC').' BTC<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 and provide the date as YYYY-MM-DD
$fixer->setEndPoint('historical','2018-01-01');

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

//the response property now contains the new response
echo '<h4>Historical rate request</h4>';
echo
'Exchange rate on '.$fixer->response->date.'<br>';
echo
'EUR to USD = '.$fixer->response->rates->USD.'<br>';
echo
'EUR to GBP = '.$fixer->response->rates->GBP.'<br>';
echo
'EUR to BTC = '.$fixer->response->rates->BTC.'<br>';

/*
Change base using the class method
*/
$fixer->changeBase('USD');

//the response property now contains the rates for the new base
echo '<h4>Historical rates after changing base to USD using class method</h4>';
echo
'Exchange rate on '.$fixer->response->date.'<br>';
echo
'USD to EUR = '.$fixer->response->rates->EUR.'<br>';
echo
'USD to GBP = '.$fixer->response->rates->GBP.'<br>';
echo
'USD to BTC = '.$fixer->response->rates->BTC.'<br>';

if(
$plan == 'basic' OR $plan == 'professional' OR $plan == 'pro-plus' ){

/*
Works in all plans except free
*/

/*
Switch base currency using the API request
*/

    //make sure our endpoint is latest
   
$fixer->setEndPoint('latest');

   
//set our base currency to the US dollar
   
$fixer->setParam('base','USD');

   
//specify the currencies by symbols that we want rates for
   
$fixer->setParam('symbols','EUR,GBP,BTC');

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

   
//we now have rates relative to the USD
   
echo '<h4>Base currency changed to the USD rates using API request</h4>';
    echo
'USD to EUR = '.$fixer->response->rates->EUR.'<br>';
    echo
'USD to GBP = '.$fixer->response->rates->GBP.'<br>';
    echo
'USD to BTC = '.$fixer->response->rates->BTC.'<br>';

/*
Conversions using the convert endpoint
*/

    //set our endpoint to convert
   
$fixer->setEndPoint('convert');

   
//reset the params so we can specify new ones
   
$fixer->resetParams();

   
//set params for conversion from USD to EUR
   
$fixer->setParam('amount',10);
   
$fixer->setParam('from','USD');
   
$fixer->setParam('to','EUR');

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

    echo
'<h4>Converting 10 USD to EUR using api request</h4>';
    echo
'10 USD = '.$fixer->response->result.' EUR<br>';

   
//convert using historical data from 1/1/2018
    //set the date to use as YYYY-MM-DD
   
$fixer->setParam('date','2018-01-01');

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

    echo
'<h4>Converting 10 USD to EUR using historical api request</h4>';
    echo
'On '.$fixer->response->date.' 10 USD = '.$fixer->response->result.' EUR<br>';

    if(
$plan == 'professional' OR $plan == 'pro-plus' ){

/*
Works in the professional and pro-plus plans
*/

        //set our endpoint to timeseries
       
$fixer->setEndPoint('timeseries');

       
//reset the params so we can specify new ones
       
$fixer->resetParams();

       
//get historical rates on USD to EUR for the first seven days in February 2018
        //you can add as may symbols as you want to get those rates in the same request
        //for this example we are only getting euros
       
$fixer->setParam('base','USD');
       
$fixer->setParam('symbols','EUR');
       
$fixer->setParam('start_date','2018-02-01');
       
$fixer->setParam('end_date','2018-02-07');

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

        echo
'<h4>Time series rates on USD to EUR for first seven days in Feb 2018</h4>';

       
//we are going to loop through the rates response
       
foreach( $fixer->response->rates as $key=>$rate ){
            echo
'USD to EUR rate on '.$key.' was '.$rate->EUR.'<br>';
        }

        if(
$plan == 'pro-plus' ){

/*
Works in the pro-plus plan
*/

            //set our endpoint to fluctuation
           
$fixer->setEndPoint('fluctuation');

           
//reset the params so we can specify new ones
           
$fixer->resetParams();

           
//get historical fluctuation of rates on USD for the month of February 2018
            //you can add as may symbols as you want to get those rates in the same request
            //for this example we are only getting euros and australian dollars
           
$fixer->setParam('base','USD');
           
$fixer->setParam('symbols','EUR,AUD');
           
$fixer->setParam('start_date','2018-02-01');
           
$fixer->setParam('end_date','2018-02-28');

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

            echo
'<h4>Fluctuation of rates on USD for the month of Feb 2018</h4>';

           
//we are going to loop through the rates response
           
foreach( $fixer->response->rates as $key=>$rate ){
                echo
'USD to '.$key.' rate on '.$fixer->response->start_date.' was '.$rate->start_rate.'<br>';
                echo
'USD to '.$key.' rate on '.$fixer->response->end_date.' was '.$rate->end_rate.'<br>';
                echo
'USD to '.$key.' change was '.$rate->change.'<br>';
                echo
'USD to '.$key.' pct change was '.$rate->change_pct.'<br><br>';
            }

        }

    }

}
?>


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

 Version Control Unique User Downloads Download Rankings  
 0%
Total:99
This week:1
All time:9,797
This week:560Up