Skip to content

Commit

Permalink
Ability to retrieve historical rates
Browse files Browse the repository at this point in the history
  • Loading branch information
infiniweb committed Jun 3, 2018
1 parent 2b4af68 commit 5c0fef2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 17 deletions.
30 changes: 17 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
# 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();

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
Expand Down Expand Up @@ -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

Expand All @@ -91,4 +96,3 @@ You will receive the following array:
[rate] => 1.222637
[result] => 30.565925
)

2 changes: 1 addition & 1 deletion src/FixerAPI/Fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 6 additions & 3 deletions src/FixerAPI/Rates.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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);
}

}

0 comments on commit 5c0fef2

Please sign in to comment.