|
| 1 | +# PHP 7 API Wrapper for Popular Currency Rate APIs |
| 2 | + |
| 3 | +[](https://packagist.org/packages/otherguy/php-currency-api) |
| 4 | +[](https://github.com/otherguy/php-currency-api/issues) |
| 5 | +[](https://travis-ci.com/otherguy/php-currency-api) |
| 6 | +[](https://coveralls.io/github/otherguy/php-currency-api?branch=master) |
| 7 | +[](LICENSE.md) |
| 8 | + |
| 9 | +Dont worry about your favorite currency conversion service suddenly shutting down or switching plans on you. Switch away easily. |
| 10 | + |
| 11 | +## Inspiration |
| 12 | + |
| 13 | +I needed a currency conversion API for [my travel website]() but couldn't find a good PHP package. The idea of the |
| 14 | +[`Rackbeat/php-currency-api`](https://github.com/Rackbeat/php-currency-api) package came closest but unfortunately it |
| 15 | +was just a stub and not implemented. |
| 16 | + |
| 17 | +## Supported APIs |
| 18 | + |
| 19 | +* [FixerIO](https://fixer.io) (`fixerio`) |
| 20 | +* [CurrencyLayer](https://currencylayer.com) (`currencylayer`) |
| 21 | +* [Open Exchange Rates](https://openexchangerates.org) (`openexchangerates`) |
| 22 | + |
| 23 | +## Prerequisites |
| 24 | + |
| 25 | +* PHP 7.1+ |
| 26 | +* An account with one of the APIs above |
| 27 | + |
| 28 | +## Installation |
| 29 | + |
| 30 | +Just require the package using composer and you're good to go! |
| 31 | + |
| 32 | +```bash |
| 33 | +$ composer require otherguy/php-currency-api |
| 34 | +``` |
| 35 | + |
| 36 | +## Usage |
| 37 | + |
| 38 | +### Initialize instance |
| 39 | + |
| 40 | +```php |
| 41 | +$api = Otherguy\Currency\API::make('fixerio'); // driver from supported drivers. |
| 42 | +``` |
| 43 | + |
| 44 | +### Set base currency (default = USD) |
| 45 | + |
| 46 | +```php |
| 47 | +$api->setBase(Otherguy\Currency\Symbol::USD); |
| 48 | +``` |
| 49 | + |
| 50 | +### Set symbols to return (default = all/[]) |
| 51 | + |
| 52 | +```php |
| 53 | +$api->setSymbols([ Otherguy\Currency\Symbol::DKK, Otherguy\Currency\Symbol::EUR, Otherguy\Currency\Symbol::USD ]); |
| 54 | +``` |
| 55 | + |
| 56 | +*Please note, you are not required to use `Otherguy\Currency\Symbol` to specify symbols. It's simply a convenience helper.* |
| 57 | + |
| 58 | +### Get latest rates |
| 59 | + |
| 60 | +```php |
| 61 | +$api->get(); // Get latest rates for selected symbols, using set base currency |
| 62 | +$api->get('DKK'); // Get latest rates for selected symbols, using DKK as base currency |
| 63 | +``` |
| 64 | + |
| 65 | +### Convert amount from one currency to another |
| 66 | + |
| 67 | +```php |
| 68 | +$api->convert($fromCurrency = 'DKK', $toCurrency = 'EUR', 10.00); // Convert 10 DKK to EUR |
| 69 | +``` |
| 70 | + |
| 71 | +### Get rate on specific date |
| 72 | + |
| 73 | +```php |
| 74 | +$api->historical($date = '2018-01-01'); // Get currency rate for base on 1st of January 2018 |
| 75 | +$api->historical($date = '2018-01-01', 'GBP'); // Get currency rate for GBP on 1st of January 2018 |
| 76 | +``` |
0 commit comments