From fdd0a03e1140201fe617acaabc3764744c2a70c0 Mon Sep 17 00:00:00 2001 From: infiniweb <17177824+infiniweb@users.noreply.github.com> Date: Sun, 3 Jun 2018 19:48:05 +0400 Subject: [PATCH] Add Time-Series Endpoint support --- README.md | 30 ++++++++++++++++++++++++++++++ src/FixerAPI/Rates.php | 35 +++++++++++++++++++++++++++++++++-- 2 files changed, 63 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 5cc3d35..8325d1a 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,10 @@ This will return a list of symbols (ISO 4217 Currency Code) as a simple array: ### 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) + +#### Real-time rates + You can get the latest rates for all or for specific currencies: $baseCurrency = "EUR"; @@ -72,12 +76,38 @@ This will return the rates of provided currencies compared to the base currency. ) ) +#### Historical rates + 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` +#### Time-series rates + +You can get daily rates from a starting end an end date, using: + + $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 + ) + ... + + ### Convert You can request the conversion from a currency to another. If you provide a date, it will return an historical rate. diff --git a/src/FixerAPI/Rates.php b/src/FixerAPI/Rates.php index 95f2fe9..d54669f 100644 --- a/src/FixerAPI/Rates.php +++ b/src/FixerAPI/Rates.php @@ -22,6 +22,22 @@ public function __construct(Fixer $fixer) * @return array */ public function get($baseCurrency = null, $symbols = [], $date = null) + { + $data = $this->prepareData($baseCurrency, $symbols); + + // If a date is provided, we will get the historical data, the endpoint needs to be changed accordingly + $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' => (array)$response->rates); + } + + public function prepareData($baseCurrency = null, $symbols = [], $startDate = null, $endDate = null) { $data = array(); @@ -33,7 +49,22 @@ public function get($baseCurrency = null, $symbols = [], $date = null) $data['symbols'] = implode(",", $symbols); } - $endPoint = $date !== null ? $date : $this->endpointKey; + if ($startDate !== null) { + $data['start_date'] = $startDate; + } + + if ($endDate !== null) { + $data['end_date'] = $endDate; + } + + return $data; + } + + public function getDailyRates($startDate, $endDate, $baseCurrency = null, $symbols = []) + { + $data = $this->prepareData($baseCurrency, $symbols, $startDate, $endDate); + + $endPoint = "timeseries"; $response = $this->fixer->getResponse($endPoint, $data); @@ -41,7 +72,7 @@ public function get($baseCurrency = null, $symbols = [], $date = null) throw new \Exception("Error Processing Request", 1); } - return array('timestamp' => $response->timestamp, 'base' => $response->base, 'rates' => (array)$response->rates); + return array('base' => $response->base, 'rates' => (array)$response->rates); } }