Skip to content
Wrapper api for currency and exchange rates
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github/ISSUE_TEMPLATE
src
tests/Unit
.gitignore
.scrutinizer.yml
.travis.yml
CODE_OF_CONDUCT.md
CONTRIBUTING.md
LICENSE
README.md
_config.yml
composer.json
composer.lock
phpunit.xml

README.md

anarchalien/exchange

Quality Score

Retrieves current and historical Currency Exchange Rates

Installation

Require this package with composer. composer require anarchalien/exchange

Testing

Run vendor/bin/phpunit tests

Laravel 5.5+:

This package should auto discover. If you don't use auto-discovery add the ServiceProvider to the providers array in config/app.php.

Anarchalien\Exchange\CurrencyExchangeServiceProvider::class,

To use the Facade register it in config/app.php

'CurrencyExchange'=> Anarchalien\Exchange\Facade::class,

Usage

This package uses 2 free to use APIs (credited below) behind the scenes, when using this package you should cache your results as much as is possible to help keep the underlying APIs free

Anarchalien\Exchange\CurrencyExchange

is the main access point, the examples below use a Laravel facade, but you could use DI to make it available in your application, the same methods will be available.

Examples with Laravel facade

Getting rates for the current or a specific day

$rates = CurrencyExchange::getRates() //Gets the latest rates(today), base currency EUR (euro) 

// Get rates for yesterday in GBP
$yesterday = \Carbon\Carbon::now()->subDays(1);
$rates = CurrencyExchange::getRates('GBP',[],$yesterday); 

// Limit the currencies we want rates for
$symbols = ['EUR','USD'];
$rates = CurrencyExchange::getRates('GBP',$symbols,$yesterday); 

//Alternately we can chain method calls to build our request
$rates = CurrencyExchange::setDate($yesterday)
  ->setBaseCurrency('GBP')
  ->setSymbols($symbols)
  ->rates();

Getting historical rates

//Get range of rates in EUR for the last week
$lastWeek = Carbon\Carbon::now()->subWeeks(1);
$today = Carbon\Carbon::now();
$rates = CurrencyExchange::getHistoricalRates($lastWeek,$today);

//Get range in GBP, limit results to EUR & USD
$symbols = ['EUR','USD'];
$rates = CurrencyExchange::getHistoricalRates($lastWeek,$today,'GBP',$symbols);

//With chained method calls
$rates = CurrencyExchange::setStart($lastWeek)
  ->setEnd($today)
  ->setBaseCurrency('GBP')
  ->setSymbols($symbols)
  ->historical();

Results

Results from CurrencyExchange::getRates() and CurrencyExchange::getHistoricalRates() will be instances of Anarchalien\Exchange\RateCollection

If the returned collection is not empty it will contain n+ objects of class Anarchalien\Exchange\Currencies\Currency

$collection = CurrencyExchange::getRates();

if(!$collection->isEmpty()){
  $currency = $collection->first(); //Currency::class
  $code = $currency->getCode(); //string
  $description = $currency->getDescription(); //string
  $base = $currency->getBase(); //string
  $rate = $currency->getRate(); //float
  $date = $currency->getDate(); //Carbon
}

//You can also convert the collection and its contents to an array
$collection->toArray();

Limitations

  • Cannot retrieve exchange rate data from before 1999.

Resources

Underlying APIs

Currently the endpoints on the APIs used do not require any authentication

License

MIT

You can’t perform that action at this time.