1- # 💱 PHP Currency API
1+ # 💱 PHP Currency Exchange Rate API
22_ A PHP 7 API Wrapper for Popular Currency Rate APIs._
33
44[ ![ Version] ( https://img.shields.io/packagist/v/otherguy/php-currency-api.svg?style=flat-square )] ( https://packagist.org/packages/otherguy/php-currency-api )
@@ -11,19 +11,17 @@ _A PHP 7 API Wrapper for Popular Currency Rate APIs._
1111Dont worry about your favorite currency conversion service suddenly shutting down or switching plans on you. Switch away easily.
1212
1313## Inspiration 💅
14-
15- I needed a currency conversion API for [ my travel website] ( ) but couldn't find a good PHP package. The idea of the
14+ I needed a currency conversion API for [ my travel website] ( ) but could not find a good PHP package. The idea of the
1615[ ` Rackbeat/php-currency-api ` ] ( https://github.com/Rackbeat/php-currency-api ) package came closest but unfortunately it
1716was just a stub and not implemented.
1817
1918## Features 🌈
20-
2119* Support for [ multiple different APIs] ( #supported-apis- ) through the use of drivers
22- * Consistent return interface, independent of the driver being used
20+ * A [ fluent interface] ( #fluent-interface ) to make retrieving exchange rates convenient and fast
21+ * Consistent return interface that is independent of the driver being used
2322* [ Calculations] ( #conversion-result ) can be made based on the returned data
2423
2524## Supported APIs 🌐
26-
2725| Service | Identifier |
2826| ------------------------------------------------------| ---------------------|
2927| [ FixerIO] ( https://fixer.io ) | ` fixerio ` |
@@ -35,13 +33,11 @@ was just a stub and not implemented.
3533_ If you want to see more services added, feel free to [ open an issue] ( https://github.com/otherguy/php-currency-api/issues ) !_
3634
3735## Prerequisites 📚
38-
3936* ` PHP 7.1 ` or higher (Tested on: PHP ` 7.1 ` ✅, ` 7.2 ` ✅ and ` 7.3 ` ✅)
4037* The [ ` composer ` ] ( https://getcomposer.org ) dependency manager for PHP
4138* An account with one or more of the [ API providers] ( #supported-apis- ) listed above
4239
4340## Installation 🚀
44-
4541Simply require the package using ` composer ` and you're good to go!
4642
4743``` bash
@@ -51,10 +47,11 @@ $ composer require otherguy/php-currency-api
5147## Usage 🛠
5248
5349### Currency Symbol Helper
54-
5550The [ ` Otherguy\Currency\Symbol ` ] ( src/Symbol.php ) class provides constants for each supported currency. This is merely
5651a helper and does not need to be used. You can simply pass strings like ` 'USD', 'EUR', ... ` to most methods.
5752
53+ > Please note that you are not required to use ` Otherguy\Currency\Symbol ` to specify symbols. It's simply a convenience helper.
54+
5855``` php
5956// 'USD'
6057$symbol = Otherguy\Currency\Symbol::USD;
@@ -82,7 +79,6 @@ $symbols = Otherguy\Currency\Symbol::name( Otherguy\Currency\Symbol::USD );
8279```
8380
8481### Initialize API Instance
85-
8682``` php
8783$currency = Otherguy\Currency\DriverFactory::make('fixerio'); // driver identifier from supported drivers.
8884```
@@ -94,11 +90,26 @@ To get a list of supported drivers, use the `getDrivers()` method:
9490$drivers = Otherguy\Currency\DriverFactory::getDrivers()
9591```
9692
97- ### Set Base Currency
93+ ### Set Access Key
94+ Most API providers require you to sign up and use your issued access key to authenticate against their API. You can
95+ specify your access key like so:
96+
97+ ``` php
98+ $currency->accessKey('your-access-token-goes-here');
99+ ```
100+
101+ ### Set Configuration Options
102+ To set further configuration options, you can use the ` config() ` method. An example is
103+ [ CurrencyLayer's JSON formatting option] ( https://currencylayer.com/documentation#format ) .
104+
105+ ``` php
106+ $currency->config('format', '1');
107+ ```
98108
109+ ### Set Base Currency
99110You can use either ` from() ` or ` source() ` to set the base currency. The methods are identical.
100111
101- > ** Note:** Each driver sets its own default base currency. [ FixerIO] ( https://fixer.io ) uses ` EUR ` as base currency
112+ > ** Note:** Each driver sets its own default base currency. [ FixerIO] ( https://fixer.io ) uses ` EUR ` as base currency
102113> while [ CurrencyLayer] ( https://currencylayer.com ) uses ` USD ` .
103114
104115Most services only allow you to change the base currency in their paid plans. The driver will throw a
@@ -110,38 +121,52 @@ $currency->from(Otherguy\Currency\Symbol::USD);
110121```
111122
112123### Set Return Currencies
124+ You can use either ` to() ` or ` symbols() ` to set the return currencies. The methods are identical. Pass a single currency
125+ or an array of currency symbols to either of these methods.
113126
114- You can use either ` to() ` or ` symbols() ` to set the return currencies. The methods are identical.
115-
127+ > ** Note:** Pass an empty array to return all currency symbols supported by this driver. This is the default if you
128+ > don't call the method at all.
129+
116130``` php
117- $api->to([ Otherguy\Currency\Symbol::BTC, Otherguy\Currency\Symbol::EUR, Otherguy\Currency\Symbol::USD ]);
131+ $currency->to(Otherguy\Currency\Symbol::BTC);
132+ $currency->symbols([Otherguy\Currency\Symbol::BTC, Otherguy\Currency\Symbol::EUR, Otherguy\Currency\Symbol::USD]);
118133```
119134
120- * Please note, you are not required to use ` Otherguy\Currency\Symbol ` to specify symbols. It's simply a convenience helper.*
121-
122- ### Get latest rates
135+ ### Latest Rates
136+ This retrieves the most recent exchange rates and returns a [ ` ConversionResult ` ] ( #conversion-result ) object.
123137
124138``` php
125- $api ->get(); // Get latest rates for selected symbols, using set base currency
126- $api ->get('DKK'); // Get latest rates for selected symbols, using DKK as base currency
139+ $currency ->get(); // Get latest rates for selected symbols, using set base currency
140+ $currency ->get('DKK'); // Get latest rates for selected symbols, using DKK as base currency
127141```
128142
129- ### Convert amount from one currency to another
143+ ### Historical Rates
144+ To retrieve historical exchange rates, use the ` historical() ` method. Note that you need to specify a date either as a
145+ method parameter or by using the ` date() ` methods. See [ Fluent Interface] ( #fluent-interface ) for more information.
130146
131147``` php
132- $api->convert($fromCurrency = 'DKK', $toCurrency = 'EUR', 10.00); // Convert 10 DKK to EUR
148+ $currency->date('2010-01-01')->historical();
149+ $currency->historical('2018-07-01');
133150```
134151
135- ### Get rate on specific date
152+ ### Convert Amount
153+ Use the ` convert() ` method to convert amounts between currencies.
154+
155+ > ** Note:** Most API providers don't allow access to this method using your free account. You can still use the
156+ > [ Latest Rates] ( #latest-rates ) or [ Historical Rates] ( #historical-rates ) endpoints and perform calculations or conversions
157+ > on the [ ` ConversionResult ` ] ( #conversion-result ) object.
136158
137159``` php
138- $api->historical($date = '2018-01-01 '); // Get currency rate for base on 1st of January 2018
139- $api->historical($date = '2018 -01-01', 'GBP' ); // Get currency rate for GBP on 1st of January 2018
160+ $currency->convert(10.00, 'USD', 'THB '); // Convert 10 USD to THB
161+ $currency->convert(122.50, 'NPR', 'EUR', '2019 -01-01'); // Convert 122.50 NPR to EUR using the rates from January 1st, 2019
140162```
141163
164+ ### Fluent Interface
165+ ` TODO `
166+
142167### Conversion Result
168+ ` TODO `
143169
144170## Contributing 🚧
145-
146171[ Pull Requests] ( https://github.com/otherguy/php-currency-api/pulls ) are more than welcome! I'm striving for 100% test
147172coverage for this repository so please make sure to add tests for your code.
0 commit comments