PHP wrapper for Fixer API
This API wrapper provides a simple way to access fixer.io API in order to easily consume the endpoints with a PHP application., (*1)
We are supporting multiple endpoints:, (*2)
-
symbols: Retrieve the list of currencies supported by Fixer
-
rates: Return real-time exchange rate data AND historical data
-
convert: Return real-time exchange rate data
Installation
You can use composer to include this package to your project:, (*3)
composer require infiniweb/fixer-api-php
Quick Start
You will need first to instanciate the Fixer class:, (*4)
$fixer = new \InfiniWeb\FixerAPI\Fixer();
And provide the Fixer API Key:, (*5)
$fixer->setAccessKey($apiKey);
Make sure to first get your Free or paid API Key here https://fixer.io/product, (*6)
You are now ready to consume the API!, (*7)
Symbols
To get the list of Symbols, simply use the following:, (*8)
$symbols = $fixer->symbols->get();
This will return a list of symbols (ISO 4217 Currency Code) as a simple array:, (*9)
Array
(
[AED] => United Arab Emirates Dirham
[AFN] => Afghan Afghani
[ALL] => Albanian Lek
...
Rates
The are various ways to get rates. It can be real-time data, historical data or series data (from a date to another date), (*10)
Real-time rates
You can get the latest rates for all or for specific currencies:, (*11)
$baseCurrency = "EUR";
$symbols = array("USD", "GBP");
$return = $fixer->rates->get($baseCurrency, $symbols);
This will return the rates of provided currencies compared to the base currency., (*12)
Array
(
[timestamp] => 1528014248
[base] => EUR
[rates] => Array
(
[USD] => 1.166583
[GBP] => 0.874168
)
)
Historical rates
You could also retrive historical rate data by including the date in the request, such as:, (*13)
$fixer->rates->get($baseCurrency, $symbols, "2018-01-19");
Note that the date needs to be following this format: YYYY-MM-DD
, (*14)
Time-series rates
You can get daily rates from a starting end an end date, using:, (*15)
$return = $fixer->rates->getDailyRates("2018-05-01", "2018-05-03", $baseCurrency, $symbols);
Array
(
[base] => EUR
[rates] => Array
(
[2018-05-01] => stdClass Object
(
[USD] => 1.199468
[GBP] => 0.881297
)
[2018-05-02] => stdClass Object
(
[USD] => 1.195602
[GBP] => 0.880967
)
...
Fluctuations
The parameters to retrieve the fluctuations are exactly the same then the time-series rates., (*16)
$return = $fixer->rates->getDailyFluctuation("2018-05-01", "2018-05-03", $baseCurrency, $symbols);
You will then get the daily flactuations for each currencies from the start to end date:, (*17)
Array
(
[base] => EUR
[rates] => Array
(
[USD] => stdClass Object
(
[start_rate] => 1.199468
[end_rate] => 1.199326
[change] => -0.0001
[change_pct] => -0.0118
)
[GBP] => stdClass Object
(
[start_rate] => 0.881297
[end_rate] => 0.883748
[change] => 0.0025
[change_pct] => 0.2781
)
)
)
Convert
You can request the conversion from a currency to another. If you provide a date, it will return an historical rate., (*18)
$from = "EUR";
$to = "USD";
$amount = "25";
$date = "2018-01-19";
$return = $fixer->convert->get($from, $to, $amount, $date);
You will receive the following array:, (*19)
Array
(
[timestamp] => 1516406399
[rate] => 1.222637
[result] => 30.565925
)
Additional features
SSL support
All paid subscription plans available on Fixer.io come with 256-bit SSL encryption. You can enable SSL support by providing extra information in the class constructor:, (*20)
$config = array('ssl' => true);
$fixer = new \InfiniWeb\FixerAPI\Fixer($config);