Skip to content

Commit

Permalink
Add Time-Series Endpoint support
Browse files Browse the repository at this point in the history
  • Loading branch information
infiniweb committed Jun 3, 2018
1 parent 5c0fef2 commit fdd0a03
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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.
Expand Down
35 changes: 33 additions & 2 deletions src/FixerAPI/Rates.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -33,15 +49,30 @@ 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);

if (!isset($response->rates)) {
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);
}

}

0 comments on commit fdd0a03

Please sign in to comment.