Download the PHP package otherguy/php-currency-api without Composer
On this page you can find all versions of the php package otherguy/php-currency-api. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download otherguy/php-currency-api
More information about otherguy/php-currency-api
Files in otherguy/php-currency-api
Package php-currency-api
Short Description A PHP API Wrapper to offer a unified programming interface for popular Currency Rate APIs.
License MIT
Informations about the package php-currency-api
💱 Wrapper for popular Currency Exchange Rate APIs
A PHP API Wrapper to offer a unified programming interface for popular Currency Rate APIs.
Dont worry about your favorite currency conversion service suddenly shutting down or switching plans on you. Switch away easily, without changing your code.
Inspiration 💅
I needed a currency conversion API for my travel website but could not find a good PHP package. The idea of the
Rackbeat/php-currency-api
package came closest but unfortunately it
was just a stub and not implemented.
Features 🌈
- Support for multiple different APIs through the use of drivers
- A fluent interface to make retrieving exchange rates convenient and fast
- Consistent return interface that is independent of the driver being used
- Calculations can be made based on the returned data
Supported APIs 🌐
Service | Identifier |
---|---|
FixerIO | fixerio |
CurrencyLayer | currencylayer |
Open Exchange Rates | openexchangerates |
Exchange Rates API | exchangeratesapi |
If you want to see more services added, feel free to open an issue!
Prerequisites 📚
PHP 8.x
orPHP 7.3+
or higher (tested on both7.3
and7.4
)- The
composer
dependency manager for PHP - An account with one or more of the API providers listed above
Installation 🚀
Simply require the package using composer
and you're good to go!
Usage 🛠
Currency Symbol Helper
The Otherguy\Currency\Symbol
class provides constants for each supported currency.
!Note: You are not required to use
Otherguy\Currency\Symbol
to specify symbols. It's simply a convenience helper and does not need to be used. You can simply pass strings like'USD', 'EUR', ...
to all methods.
Use the all()
method to retrieve an array of all currency symbols:
The names()
method returns an associative array with currency names instead:
To get the name of a single currency, use the name()
method:
Initialize API Instance
To get a list of supported drivers, use the getDrivers()
method:
Set Access Key
Most API providers require you to sign up and use your issued access key to authenticate against their API. You can specify your access key like so:
Set Configuration Options
To set further configuration options, you can use the config()
method. An example is
CurrencyLayer's JSON formatting option.
Set Base Currency
You can use either from()
or source()
to set the base currency. The methods are identical.
!Note: Each driver sets its own default base currency. FixerIO uses
EUR
as base currency while CurrencyLayer usesUSD
.
Most services only allow you to change the base currency in their paid plans. The driver will throw a
Otherguy\Currency\Exceptions\ApiException
if your current plan does not allow changing the base currency.
Set Return Currencies
You can use either to()
or symbols()
to set the return currencies. The methods are identical. Pass a single currency
or an array of currency symbols to either of these methods.
!Note: Pass an empty array to return all currency symbols supported by this driver. This is the default if you don't call the method at all.
Latest Rates
This retrieves the most recent exchange rates and returns a ConversionResult
object.
Historical Rates
To retrieve historical exchange rates, use the historical()
method. Note that you need to specify a date either as a
method parameter or by using the date()
methods. See Fluent Interface for more information.
Convert Amount
Use the convert()
method to convert amounts between currencies.
!Note: Most API providers don't allow access to this method using your free account. You can still use the Historical Rates endpoints and perform calculations or conversions on the
ConversionResult
object.
Fluent Interface
Most methods can be used with a fluent interface, allowing you to chain method calls for more readable code:
Conversion Result
The historical()
endpoints return a
ConversionResult
object. This object allows you to perform calculations and
conversions easily.
!Note: Even though free accounts of most providers do not allow you to change the base currency, you can still use the
ConversionResult
object to change the base currency later. This might not be as accurate as changing the base currency directly, though.!Note: To convert between two currencies, you need to request both of them in your initial
get()
orhistorical()
request. You can not convert between currencies that have not been fetched!
See the following code for some examples of what you can do with the ConversionResult
object.
Contributing 🚧
Pull Requests are more than welcome! I'm striving for 100% test coverage for this repository so please make sure to add tests for your code.