Download the PHP package cmbuckley/exchange-rates without Composer
On this page you can find all versions of the php package cmbuckley/exchange-rates. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download cmbuckley/exchange-rates
More information about cmbuckley/exchange-rates
Files in cmbuckley/exchange-rates
Package exchange-rates
Short Description A package for interacting with the exchangerate.host API
License MIT
Homepage https://github.com/cmbuckley/exchange-rates
Informations about the package exchange-rates
Table of Contents
- Overview
- Installation
- Usage
- Setup
- Methods
- Available Currencies
- Exchange Rate Between Two Currencies
- Exchange Rate Between More Than Two Currencies
- Exchange Rates Between Date Range
- Convert Currencies
- Convert Currencies Between Date Range
- Testing
- Contribution
- Credits
- Changelog
- License
Overview
Exchange Rates is a simple PHP package used for interacting with the exchangerate.host API. You can use it to get the latest or historical exchange rates and convert monetary values between different currencies and cryptocurrencies.
Installation
You can install the package via Composer:
The package has been developed and tested to work with the following minimum requirements:
- PHP 8.2
Usage
Setup
The exchangerate.host service requires an access key, which you can sign up and request for free. You need to add a payment card to get the key, but you won't be charged if you're on the free plan. You are resticted to 100 requests per month on the free plan.
Set the API key as follows:
The API also does not support HTTPS on the free plan. If you are on a paid plan and want to use HTTPS, pass the tls
service option:
Methods
Available Currencies
To get the available currencies that are supported by the package, you can use the currencies()
method like so:
Exchange Rate Between Two Currencies
To get the exchange for one currency to another, you can use the exchangeRate()
method.
The example below shows how to get the exchange rate from GBP to EUR for today:
If a valid date is passed as the third parameter, the exchange rate for that day will be returned. If no date is passed, today's exchange rate will be used.
Exchange Rate Between More Than Two Currencies
It is possible to get the exchange rates for multiple currencies in one call. This can be particularly useful if you are needing to get many exchange rates at once and do not want to make multiple API calls.
To do this, you can use exchangeRate()
method and pass an array of currency code strings as the second parameter. This will return an array containing the exchange rates as strings:
As above, you can pass a date as the third parameter to get the exchange rates for that day.
Exchange Rates Between Date Range
To get the exchange rates between two currencies and a range of dates, you can use the exchangeRateBetweenDateRange()
method.
The example below shows how to get the exchange rates from GBP to EUR for the past 3 days:
As before, you can pass an array of currency codes as the second parameter:
Convert Currencies
Similar to how you can get the exchange rate from one currency to another, you can also convert a monetary value from one currency to another. To do this you can use the convert()
method.
When passing in the monetary value (first parameter) that is to be converted, it's important that you pass it in the lowest denomination of that currency. For example, £1 GBP would be passed in as 100 (as £1 = 100 pence).
The example below shows how to convert £1 to EUR at today's exchange rate:
If a valid date is passed as the third parameter, the exchange rate for that day will be used. If no date is passed, today's exchange rate will be used.
You can also use the convert()
method to convert a monetary value from one currency to multiple currencies. To do this, you can pass an array of currency codes strings as the third parameter:
Convert Currencies Between Date Range
Similar to getting the exchange rates between a date range, you can also get convert monetary values from one currency to another using the exchange rates. To do this you can use the convertBetweenDateRange()
method:
You can also convert to multiple currencies:
Testing
To run the tests for the package, you can use the following command:
Contribution
If you wish to make any changes or improvements to the package, feel free to make a pull request.
To contribute to this library, please use the following guidelines before submitting your pull request:
- Write tests for any new functions that are added. If you are updating existing code, make sure that the existing tests pass and write more if needed.
- Follow PSR-12 coding standards.
- Make all pull requests to the
main
branch.
Credits
Changelog
Check the CHANGELOG to get more information about the latest changes.
License
The MIT License (MIT). Please see License File for more information.