From 5c0fef23887e2050fe09fa38f56e82f4ae318d79 Mon Sep 17 00:00:00 2001 From: infiniweb <17177824+infiniweb@users.noreply.github.com> Date: Sun, 3 Jun 2018 19:22:14 +0400 Subject: [PATCH] Ability to retrieve historical rates --- README.md | 30 +++++++++++++++++------------- src/FixerAPI/Fixer.php | 2 +- src/FixerAPI/Rates.php | 9 ++++++--- 3 files changed, 24 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 8bc1ce7..5cc3d35 100644 --- a/README.md +++ b/README.md @@ -1,32 +1,30 @@ # PHP wrapper for Fixer API -This API wrapper provide a simple way to access to [fixer.io API](https://fixer.io/documentation) in order to easily consume Fixer endpoints. +This API wrapper provides a simple way to access [fixer.io API](https://fixer.io/documentation) in order to easily consume the endpoints with a PHP application. --- -- [Installation](#installation): Retrieve the list of currencies supported by Fixer -- [QuickStart](#quick-start): Retrieve the list of currencies supported by Fixer +- [Installation](#installation) +- [QuickStart](#quick-start) -We are supporting multiple endpoints +We are supporting multiple endpoints: -- [/symbols](#symbols): Retrieve the list of currencies supported by Fixer -- [/latest (Rates)](#rates): Return real-time exchange rate data -- [/convert](#convert): Return real-time exchange rate data +- [symbols](#symbols): Retrieve the list of currencies supported by Fixer +- [rates](#rates): Return real-time exchange rate data AND historical data +- [convert](#convert): Return real-time exchange rate data --- ### Installation -The package can be included with composer: +You can use composer to include this package to your project: composer require infiniweb/fixer-api-php ### Quick Start -You need to get your free API Key [here https://fixer.io/product](https://fixer.io/product) - -Once included to your project, instanciate the Fixer class: +You will need first to instanciate the Fixer class: $fixer = new \InfiniWeb\FixerAPI\Fixer(); @@ -34,6 +32,8 @@ And provide the Fixer API Key: $fixer->setAccessKey($apiKey); +Make sure to first get your Free or paid API Key [here https://fixer.io/product](https://fixer.io/product) + You are now ready to consume the API! ### Symbols @@ -65,13 +65,18 @@ This will return the rates of provided currencies compared to the base currency. ( [timestamp] => 1528014248 [base] => EUR - [rates] => stdClass Object + [rates] => Array ( [USD] => 1.166583 [GBP] => 0.874168 ) ) +You could also retrive historical rate data by including the date in the request, such as: + + $fixer->rates->get($baseCurrency, $symbols, "2018-01-19"); + +Note that the date needs to be following this format: `YYYY-MM-DD` ### Convert @@ -91,4 +96,3 @@ You will receive the following array: [rate] => 1.222637 [result] => 30.565925 ) - diff --git a/src/FixerAPI/Fixer.php b/src/FixerAPI/Fixer.php index 3e7d9ff..d512be3 100644 --- a/src/FixerAPI/Fixer.php +++ b/src/FixerAPI/Fixer.php @@ -8,7 +8,7 @@ class Fixer * The API base URL * @var string */ - private $baseUrl = "http://data.fixer.io/api/"; + private $baseUrl = "https://data.fixer.io/api/"; /** * The API provider for GET operations diff --git a/src/FixerAPI/Rates.php b/src/FixerAPI/Rates.php index ddc4372..95f2fe9 100644 --- a/src/FixerAPI/Rates.php +++ b/src/FixerAPI/Rates.php @@ -18,9 +18,10 @@ public function __construct(Fixer $fixer) * Based on subscription * @param string|null $baseCurrency the three-letter currency code of the base currency. * @param array $symbols list of comma-separated currency codes to limit output currencies. + * @param string $date To retrive the historical value, format YYYY-MM-DD * @return array */ - public function get($baseCurrency = null, $symbols = []) + public function get($baseCurrency = null, $symbols = [], $date = null) { $data = array(); @@ -32,13 +33,15 @@ public function get($baseCurrency = null, $symbols = []) $data['symbols'] = implode(",", $symbols); } - $response = $this->fixer->getResponse($this->endpointKey, $data); + $endPoint = $date !== null ? $date : $this->endpointKey; + + $response = $this->fixer->getResponse($endPoint, $data); if (!isset($response->rates)) { throw new \Exception("Error Processing Request", 1); } - return array('timestamp' => $response->timestamp, 'base' => $response->base, 'rates' => $response->rates); + return array('timestamp' => $response->timestamp, 'base' => $response->base, 'rates' => (array)$response->rates); } }