/swap

Exchange rates library for PHP 5.3+

Primary LanguagePHPMIT LicenseMIT

Swap Build status Version License

Swap helps you to retrieve exchange rates from various providers. It leverages their ability to retrieve multiple quotes at once, while simulating this behavior for those who don't support it by sending parallel HTTP requests.

If you want to use this library with Symfony2, you can install [FlorianvSwapBundle] (https://github.com/florianv/FlorianvSwapBundle).

Installation

Add this line to your composer.json file:

{
    "require": {
        "florianv/swap": "~1.0"
    }
}

Currently Guzzle 3 and 4 are supported HTTP clients, so you will need to require one of them too.

  • "guzzle/guzzle": "~3.0"
  • "guzzlehttp/guzzle": "~4.0"

Usage

First, you need to create an adapter:

// Creating a Guzzle 3 adapter
$adapter = new \Swap\Adapter\Guzzle3Adapter(new \Guzzle\Http\Client());

// Creating a Guzzle 4 adapter
$adapter = new \Swap\Adapter\Guzzle4Adapter(new \GuzzleHttp\Client());

For BC reasons, it is still possible to pass $adapter = new \Guzzle\Http\Client(); as adapter but it will be removed in version 2.0.

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

// Creating a YahooFinance provider
$yahoo = new \Swap\Provider\YahooFinance($adapter);

// Instantiating Swap and adding the provider
$swap = new \Swap\Swap();
$swap->addProvider($yahoo);

Now, your job is to create a currency pair and Swap will set its rate:

// Creating the currency pair EUR/USD
$pair = \Swap\Model\CurrencyPair::createFromString('EUR/USD');

// Quoting the pair
$swap->quote($pair);

// 1.3751
echo $pair;

// 1.3751
echo $pair->getRate();

We created a currency pair EUR/USD, quoted it with the YahooFinance provider and got 1.3751 as rate which means that 1 EUR is exchanged for 1.3751 USD.

Currencies are expressed as their ISO 4217 code.

Multiple pairs

You can also quote multiple pairs at once:

use Swap\Model\CurrencyPair;

$eurUsd = CurrencyPair::createFromString('EUR/USD');
$usdGbp = CurrencyPair::createFromString('USD/GBP');
$gbpJpy = CurrencyPair::createFromString('GBP/JPY');

$swap->quote(array($eurUsd, $usdGbp, $gbpJpy));

// 1.3751
echo $eurUsd;

// 0.5938
echo $usdGbp;

// 171.5772
echo $gbpJpy;

Date

You can also retrieve the date at which the rate was calculated:

// $date is a \DateTime instance
$date = $pair->getDate()

Chained providers

Providers can be chained so when one of them fails, the next one can be used to quote the pairs that were not processed.

$yahoo = new \Swap\Provider\YahooFinance($client);
$google = new \Swap\Provider\GoogleFinance($client);

$swap->addProvider($yahoo);
$swap->addProvider($google);

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.

License

MIT