Currency Exchange rates library for PHP
$ composer require tautiz/CuEx
Add service to App providers array:
CuEx\CuExServiceProvider::class,
Create an HTTP adapter
$httpAdapter = new \Ivory\HttpAdapter\FileGetContentsHttpAdapter();
Then, you can create a provider and add it to CuEx:
// Create the Yahoo Finance provider
$provider = new \CuEx\Provider\YahooFinanceProvider($httpAdapter);
// Create CuEx with the provider
$cuex = new \CuEx\CuEx($provider);
To retrieve the latest exchange rate for a currency pair, you need to use the quote()
method.
$rate = $cuex->getRate('EUR/LTL');
// 3.4528
echo $rate;
// 3.4528
echo $rate->getValue();
// 15-11-28 23:59:59
echo $rate->getDate()->format('Y-m-d H:i:s');
Currencies are expressed as their ISO 4217 code.
It is possible to chain providers in order to use fallbacks in case the main providers don't support the currency or are unavailable.
Simply create a ChainProvider
wrapping the providers you want to chain.
$chainProvider = new \CuEx\Provider\ChainProvider([
new \CuEx\Provider\YahooFinanceProvider($httpAdapter),
new \CuEx\Provider\CurrencylayerProvider($httpAdapter,'<YOUR_ACCESS_KEY>'),
new \CuEx\Provider\FixerProvider($httpAdapter),
new \CuEx\Provider\GoogleFinanceProvider($httpAdapter),
]);
The rates will be first fetched using the Yahoo Finance provider and will fallback to Google Finance.
For performance reasons you might want to cache the rates during a given time.
$ composer require doctrine/cache
// Create the cache adapter
$cache = new \CuEx\Cache\DoctrineCache(new \Doctrine\Common\Cache\ApcCache(), 3600);
// Pass the cache to CuEx
$cuex = new \CuEx\CuEx($provider, $cache);
All rates will now be cached to 3600 minutes.
CuEx provides an enumeration of currency codes so you can use autocompletion to avoid typos.
use \CuEx\Util\CurrencyCodes;
// Retrieving the EUR/LTL rate
$rate = $cuex->quote(new \CuEx\Model\CurrencyPair(
CurrencyCodes::ISO_EUR,
CurrencyCodes::ISO_LTL
));