PHP Classes

File: example.php

Recommend this page to a friend!
  Classes of Dave Smith   PHP Get Currency Rates with Fixer API   example.php   Download  
File: example.php
Role: Example script
Content type: text/plain
Description: Usage Example
Class: PHP Get Currency Rates with Fixer API
Convert currency amounts at present and past times
Author: By
Last change:
Date: 6 years ago
Size: 7,614 bytes
 

Contents

Class file image Download
<?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>';
            }

        }

    }

}
?>