forexapi / client
Library for consuming the ForexAPI. Ideal for those in the financial or e-commerce sectors, this client simplifies the process of incorporating real-time forex data into various PHP-based projects.
Fund package maintenance!
Other
Requires
- php: >=7.4
- ext-json: *
- php-http/discovery: ^1.19
- psr/http-client-implementation: ^1.0
- psr/http-factory: ^1.0
- psr/http-factory-implementation: ^1.0
- psr/http-message: ^2.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.4
- nyholm/psr7: ^1.8
- php-http/curl-client: ^2.3
- php-http/message: ^1.16
- php-http/mock-client: ^1.6
- php-http/vcr-plugin: ^1.2
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^8.5
This package is auto-updated.
Last update: 2025-03-29 01:08:04 UTC
README
ForexAPI PHP Client
This is a PHP client for the ForexAPI. It provides an easy-to-use interface for interacting with the API's endpoints.
The ForexAPI offers a free plan and provides foreign exchange rates and currency conversion.
The API documentation can be found at https://forexapi.eu/en/docs.
Requirements
- PHP 7.4 or higher
- json extension
- Composer
Installation
Use Composer to install the ForexAPI PHP Client:
composer require forexapi/client
This package does not come with a Http Client. You can use any PSR-18 compatible client.
If you have multiple Http Clients installed, you can specify which one to use.
composer require guzzlehttp/guzzle forexapi/client
Usage
Create an instance of the Client
class directly with your API key:
use ForexAPI\Client\Client; $client = new Client('your-api-key');
Create new instance using the ForexAPI\Client\ForexAPIClientBuilder
class:
use \ForexAPI\Client\ForexAPIClientBuilder; $builder = (new ForexAPIClientBuilder()) ->withApiKey('your-api-key') ->withBaseUri('https://forexapi.eu/api/') ->withHttpAdapter($yourCustomHttpAdapter) ->build() ;
Using your own PSR-18 client and PSR-17 request factory is optional. If you don't provide them, the client will try to discover them automatically.
use \ForexAPI\Client\ForexAPIClientBuilder; $builder = (new ForexAPIClientBuilder()) ->withApiKey('your-api-key') ->withPsr18Client($yourCustomPsr18Client) ->withPsr17RequestFactory($yourCustomPsr17RequestFactory) ->build() ;
Get Live Quote
To get a live quote for a specific currency pair:
$quote = $client->getLiveQuote('USD', 'PLN'); echo $quote->getBase(); // USD echo $quote->getCounter(); // PLN echo $quote->getBid(); // Bid price echo $quote->getAsk(); // Ask price echo $quote->getMid(); // Mid price echo $quote->getTimestamp(); // Timestamp
Get Exchange Rate
To get the exchange rate between two currencies:
$exchangeRate = $client->getExchangeRate('USD', 'PLN'); echo $exchangeRate->getFrom(); // USD echo $exchangeRate->getTo(); // PLN echo $exchangeRate->getRate(); // Exchange rate echo $exchangeRate->getTimestamp(); // Timestamp
If you would like to get multiple exchange rates at once, you can use the getExchangeRates
method.
It accepts an array of counter currencies as an argument:
$exchangeRate = $client->getExchangeRates('USD', ['PLN', 'EUR', 'GBP']);
Convert Currency
To convert an amount from one currency to another:
$conversion = $client->convert('USD', 'PLN', 100.0); echo $conversion->getFrom(); // USD echo $conversion->getTo(); // PLN echo $conversion->getAmount(); // 100.0 echo $conversion->getResult(); // Converted amount in PLN echo $conversion->getTimestamp(); // Timestamp
If you would like to convert to multiple currencies at once, you can use the convertMany
method:
$conversions = $client->convertMany('USD', ['PLN', 'EUR', 'GBP'], 100.0);
Testing
This library includes a suite of unit tests. Run them using PHPUnit:
./vendor/bin/phpunit
License
This project is licensed under the MIT License. See the LICENSE file for details.