Giter Club home page Giter Club logo

swap's Introduction

Swap Build status Version PHP Version Total Downloads

Exchange rates library for PHP

Installation

$ composer require florianv/swap

Usage

First, you need to create an HTTP adapter provided by the egeloen/ivory-http-adapter library.

$httpAdapter = new \Ivory\HttpAdapter\FileGetContentsHttpAdapter();

Then, you can create a provider and add it to Swap:

// Create the Yahoo Finance provider
$yahooProvider = new \Swap\Provider\YahooFinanceProvider($httpAdapter);

// Create Swap with the provider
$swap = new \Swap\Swap($yahooProvider);

Quoting

To retrieve the latest exchange rate for a currency pair, you need to use the quote() method.

$rate = $swap->quote('EUR/USD');

// 1.187220
echo $rate;

// 1.187220
echo $rate->getValue();

// 15-01-11 21:30:00
echo $rate->getDate()->format('Y-m-d H:i:s');

Currencies are expressed as their ISO 4217 code.

Chaining providers

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 \Swap\Provider\ChainProvider([
    new \Swap\Provider\YahooFinanceProvider($httpAdapter),
    new \Swap\Provider\GoogleFinanceProvider($httpAdapter)
]);

The rates will be first fetched using the Yahoo Finance provider and will fallback to Google Finance.

Caching

For performance reasons you might want to cache the rates during a given time.

Doctrine Cache

Installation
$ composer require doctrine/cache
Usage
// Create the cache adapter
$cache = new \Swap\Cache\DoctrineCache(new \Doctrine\Common\Cache\ApcCache(), 3600);

// Pass the cache to Swap
$swap = new \Swap\Swap($provider, $cache);

All rates will now be cached in APC during 3600 seconds.

Illuminate Cache

Installation
$ composer require illuminate/cache
Usage
// Create the cache adapter
$store = new \Illuminate\Cache\ApcStore(new \Illuminate\Cache\ApcWrapper());
$cache = new \Swap\Cache\IlluminateCache($store, 60);

// Pass the cache to Swap
$swap = new \Swap\Swap($provider, $cache);

All rates will now be cached in APC during 60 minutes.

Currency Codes

Swap provides an enumeration of currency codes so you can use autocompletion to avoid typos.

use \Swap\Util\CurrencyCodes;

// Retrieving the EUR/USD rate
$rate = $swap->quote(new \Swap\Model\CurrencyPair(
    CurrencyCodes::ISO_EUR,
    CurrencyCodes::ISO_USD
));

Providers

  • European Central Bank Supports only EUR as base currency.
  • Google Finance Supports multiple currencies as base and quote currencies.
  • Open Exchange Rates Supports only USD as base currency for the free version and multiple ones for the enterprise version.
  • Xignite You must have access to the XigniteGlobalCurrencies API. Supports multiple currencies as base and quote currencies.
  • Yahoo Finance Supports multiple currencies as base and quote currencies.
  • WebserviceX Supports multiple currencies as base and quote currencies.
  • National Bank of Romania Supports only RON as base currency.
  • Array Retrieves rates from a PHP array

Integrations

License

MIT

swap's People

Contributors

avataru avatar florianv avatar necenzurat avatar pborreli avatar tyx avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.