All Projects → benmajor → ExchangeRatesAPI

benmajor / ExchangeRatesAPI

Licence: MIT license
An unofficial SDK for the ExchangeRatesAPI.

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to ExchangeRatesAPI

bitprices
command-line tool that generates a report of transactions with the USD (fiat) value at the time of each transaction.
Stars: ✭ 17 (-46.87%)
Mutual labels:  exchange-rate
Fixer
A foreign exchange rates and currency conversion API
Stars: ✭ 2,545 (+7853.13%)
Mutual labels:  exchange-rate
Money
A Ruby Library for dealing with money and currency conversion.
Stars: ✭ 2,451 (+7559.38%)
Mutual labels:  exchange-rate
rbcb
R interface to Brazilian Central Bank web services
Stars: ✭ 63 (+96.88%)
Mutual labels:  exchange-rate
cotizacion
Portal to find current exchange rates in Paraguay
Stars: ✭ 11 (-65.62%)
Mutual labels:  exchange-rate

ExchangeRatesAPI - Currency Exchange Rates API SDK

Latest Version Packagist Software License

This is an unofficial wrapper for the awesome, free ExchangeRatesAPI, which provides exchange rate lookups courtesy of the Central European Bank. It features a number of useful functions and can be installed easily using Composer.

4.0.0 update:
Following pricing changes for ExchangeRatesAPI, this project now uses ExchangeRates.host as its API provider. This should now be considered an unofficial SDK for . 4.0.0 should be backwards compatible with older versions which relied on the no-longer-free ExchangeRatesAPI.

Table of Contents:

  1. Installation
  2. Getting Started
  3. API Reference
  4. Supported Currencies
  5. Supported data sources
  6. Requirements
  7. Bugs & Features
  8. License

1. Installation:

The easiest installation method is to use Composer:

$ composer require benmajor/exchange-rates-api

Alternatively, you can download all files from the src/ directory and include them in your project. Important note: if you're manually installing the SDK, you must also install Guzzle Client.

2. Getting Started:

Since the CurrencyExchangeAPI does not require API keys or authentication in order to access and interrogate its API, getting started with this library is easy. The following examples show how to achieve various functions using the library.

Basic usage:
Fetch the latest exchange rates from the European Central Bank:

use \BenMajor\ExchangeRatesAPI\ExchangeRatesAPI;
use \BenMajor\ExchangeRatesAPI\Response;
use \BenMajor\ExchangeRatesAPI\Exception;

$access_key = '<YOUR ACCESS KEY>';
$use_ssl = false; # Free plans are restricted to non-SSL only.

$lookup = new ExchangeRatesAPI($access_key, $use_ssl);
$rates  = $lookup->fetch();

Historical data:
Get historical rates for any day since 1999:

$access_key = '<YOUR ACCESS KEY>';

$lookup = new ExchangeRatesAPI($access_key);
$rates  = $lookup->setFetchDate('2015-01-20')->fetch();

Get historical rates for a time period:

$access_key = '<YOUR ACCESS KEY>';

$lookup = new ExchangeRatesAPI($access_key);
$rates  = $lookup->addDateFrom('2015-01-20')->addDateTo('2015-01-21')->fetch();

Set the base currency:
By default, the base currency is set to Euro (EUR), but it can be changed:

$access_key = '<YOUR ACCESS KEY>';

$lookup = new ExchangeRatesAPI($access_key);
$rates  = $lookup->setBaseCurrency('GBP')->fetch();

Fetch specific rates:
If you do not want all current rates, it's possible to specify only the currencies you want using addRate(). The following code fetches only the exchange rate between GBP and EUR:

$access_key = '<YOUR ACCESS KEY>';

$lookup = new ExchangeRatesAPI($access_key);
$rates  = $lookup->addRate('EUR')->setBaseCurrency('GBP')->fetch();

Please refer to the API website for further information and full API docs.

Please note: By default, the fetch() method will return a new ExchangeRatesAPI\Response object. However, by passing a single argument of true to the fetch() method, you can retrieve a raw JSON resposne instead.

3. API Reference:

The following API reference lists the publicly-available methods for the

ExchangeRatesAPI Reference:

addDateFrom( string $from ):
Set the date from which to retrieve historic rates. $from should be a string containing an ISO 8601 date.

getDateFrom():
Returns the specified date from which to retrieve historic rates. Returns null if none is specified.

removeDateFrom():
Removes the specified start date for the retrieval of historic rates.

addDateTo( string $to ):
Set the end date for the retrieval of historic rates. $to should be a string containing an ISO 8601 date.

getDateTo():
Returns the specified end date for the retrieval of historic rates. Returns null if none is specified.

getAccessKey():
Returns the access_key that is currently in use.

getUseSSL():
Returns a boolean flag that determines which API URL will be used to perform requests. Free plans are restricted to non-SSL usage.

getSource():
Returns the specified data source (other than the API default) from which to retrieve rates. Returns null if none is specified.

removeDateTo():
Removes the specified end date for the retrieval of historic rates.

currencyIsSupported( string $code ):
Checks if a specific currency code is supported. $code should be passed as an ISO 4217 code (e.g. EUR).
Returns true if supported, or false if not.

sourceIsSupported( string $source ):
Checks if a specific data source is supported.
Returns true if supported, or false if not.

setBaseCurrency( string $code ):
Set the base currency to be used for exchange rates. $code should be passed an ISO 4217 code (e.g. EUR).
$code must be one of the supported currency codes.

getBaseCurrency():
Returns the currently specified base currency. If setBaseCurrency() hasn't been called, this will return the default base currency EUR.

addRates( array $codes ):
Adds multiple currencies to be retrieved. $codes should be an array of ISO 4217 codes (e.g. ["EUR", "GBP", "BGN"]).
Each code in the array must be of the supported currency codes.

addRate( string $code ):
Adds a new currency to be retrieved. $code should be passed an ISO 4217 code (e.g. EUR).
$code must be one of the supported currency codes.
If no rates are added, all rates will be returned.

removeRates( array $codes ):
Removes multiple currencies that has already been added to the retrieval list. $codes should be an array of ISO 4217 codes (e.g. ["EUR", "GBP", "BGN"]).
$code must be one of the supported currency codes.

removeRate( string $code ):
Removes a currency that has already been added to the retrieval list. $code should be passed an ISO 4217 code (e.g. EUR).
$code must be one of the supported currency codes.

setAccessKey( string $access_key ):
Sets access_key to be used in all requests.

setUseSSL( bool $use_ssl ):
Sets the API URL according to the selected mode (SSL or non-SSL). Free plans are restricted to non-SSL usage.

setSource( string $source = null ):
Sets the specified data source (other than the API default) from which to retrieve rates. Calling with no arguments resets the source to the API default.
If provided as a string, $source must be one of the supported data sources.

fetch( bool $returnJSON = false, bool $parseJSON = true ):
Send off the request to the API and return either a Response object, or the raw JSON response. If $returnJSON is set to true, a standard PHP object will be returned, rather than the ExchangeRatesAPI\Response object.

convert( string $to, float $amount, int $rounding ):
A convenience function to combine several methods in order to quickly perform a currency conversion:

$to: should be specified as the ISO 4217 currency code to convert to.
$amount: the amount to be converted.
$rounding: the amount to round the conversion. Default is 2.

This method call will return the converted currency amount in $to from the specified based currency.

getSupportedCurrencies( string $concat = null ):
Returns a list of supported currency codes. If $concat is null, an array of currency codes is returned. If $concat is specified as a string, a string will be returned, joined by $concat.

getRates( string $concat = null ):
Returns a list of the currently specified rates to retrieve. If $concat is null, an array of currency codes is returned. If $concat is specified as a string, a string will be returned, joined by $concat.

Response Reference:

This is the default object that is returned from the fetch(). Here is a list of the available methods on the Response object:

getStatusCode():
Returns the status code of the request (generally 200).

getTimestamp():
Returns the timestamp (formatted in ISO 8601 notation) the response was generated.

getBaseCurrency():
Returns the base currency of the request. If not base currency was specified using setBaseCurrency on the request, this will return the default (EUR).

getRates():
Returns a key/value pair array of the exchange rates that match against the request, for example:

[ 'GBP' => 1, 'EUR' => 1.1 ]

getRate( string $code ):
Retrieves the exchange rate for a specific currency, or returns the exchange rate if only one rate is present in the response.

getDate():
Retrieves the date returned as part of the response (formatted as Y-m-d). If response has no date property, returns null.

4. Supported Currencies:

The library supports any currency currently available on the European Central Bank's web service, which can be found here.

5. Supported data sources:

The library supports the following data sources other than the API default.

6. Requirements:

This library requires PHP >= 7.0. No other platform requirements exist, but the library is dependent on Guzzle.

7. Bugs & Features:

If you have spotted any bugs, or would like to request additional features from the library, please file an issue via the Issue Tracker on the project's Github page: https://github.com/benmajor/ExchangeRatesAPI/issues.

8. License:

Licensed under the MIT License:

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].