florianv/swap

Exchange rates library for PHP

Maintainers

Details

github.com/florianv/swap

Source

Issues

Installs: 5 131 836

Dependents: 16

Suggesters: 7

Security: 0

Stars: 1 313

Watchers: 36

Forks: 139

Open Issues: 0

4.3.0 2020-12-28 10:14 UTC

README

Build status Total Downloads Version

Swap allows you to retrieve currency exchange rates from various services such as Fixer, Currency Data or Exchange Rates Data and optionally cache the results. It is integrated to other libraries like moneyphp/money and provides a Symfony Bundle and a Laravel Package.

Sponsors

QuickStart

$ composer require php-http/curl-client nyholm/psr7 php-http/message florianv/swap
use Swap\Builder;

// Build Swap
$swap = (new Builder())

    // Use the Fixer service as first level provider
    ->add('apilayer_fixer', ['api_key' => 'Get your key here: https://fixer.io/'])
     
    // Use the currencylayer service as first fallback
    ->add('apilayer_currency_data', ['api_key' => 'Get your key here: https://currencylayer.com'])
    
    // Use the exchangerates service as second fallback
    ->add('apilayer_exchange_rates_data', ['api_key' => 'Get your key here: https://exchangeratesapi.io/'])
->build();
    
// Get the latest EUR/USD rate
$rate = $swap->latest('EUR/USD');

// 1.129
$rate->getValue();

// 2016-08-26
$rate->getDate()->format('Y-m-d');

// Get the EUR/USD rate 15 days ago
$rate = $swap->historical('EUR/USD', (new \DateTime())->modify('-15 days'));

We recommend to use the services that support our project, providing a free plan up to 100 requests per month.

Documentation

The documentation for the current branch can be found here.

Services

Here is the list of the currently implemented services:

Additionally, you can add your own services as long as they implement the ExchangeRateService interface.

Integrations

Credits

License

The MIT License (MIT). Please see LICENSE for more information.