CurrencyAPI#
This service retrieves the data from CurrencyAPI.
Installation#
Install the service:
composer require peso/currencyapi-service
Install the service with all recommended dependencies:
composer require peso/currencyapi-service php-http/discovery guzzlehttp/guzzle symfony/cache
Usage#
Added in version 2.0: Conversion requests support
- class Peso\Services\CurrencyApiService#
The service
- __construct($apiKey, $subscription[, $symbols, $multiconversion[, $cache, $ttl, $httpClient, $requestFactory]])#
Required params:
- Parameters:
$apiKey (
string) – The API key you received from the service.$subscription (
Subscription) – Subscription type, Free or Paid.
Configuration:
- Parameters:
$symbols (
array|null) – Use this list to limit currencies in the query (limits only quote/target currencies) Default:nullqueries all currencies.$multiconversion (
bool) – Enable if you need to convert a single amount of a single currency to multiple currenices. The service will ask the backend to get result for all currencies (controlled by$symbols) and the subsequent requests will get results from the cache. Only valid for conversion requests and requires caching enabled. Default:false.
Services:
- Parameters:
$cache (
CacheInterface) – PSR-16 Cache Instance. Default: no cache (not recommended).$ttl (
DateInterval) – Cache TTL. Default:1 hour.$httpClient (
ClientInterface) – PSR-18 Client Instance. Default: something discovered (requiresphp-http/discoveryand an implementation installed).$requestFactory (
RequestFactoryInterface) – PSR-17 RequestFactory Instance. Default: something discovered (requiresphp-http/discoveryand an implementation installed).
- enum Peso\Services\CurrencyApiService\Subscrtiption#
- constant Free#
Free subscription (no conversion requests)
- constant Paid#
Any type of paid subscription (conversion requests)
Example:
<?php
use Peso\Peso\CurrencyConverter;
use Peso\Services\CurrencyApiService;
use Peso\Services\CurrencyApiService\Subscription;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
use Symfony\Component\Cache\Psr16Cache;
$cache = new Psr16Cache(new FilesystemAdapter(directory: __DIR__ . '/cache'));
$service = new CurrencyApiService('...', Subscription::Free, cache: $cache);
$converter = new CurrencyConverter($service);
// 10760.13 as of 2025-07-18
echo $converter->convert('12500', 'USD', 'EUR', 2), PHP_EOL;
// you can optionally limit the retrieved symbols
$cache = new Psr16Cache(new FilesystemAdapter(directory: __DIR__ . '/cache'));
$service = new CurrencyApiService('...', Subscription::Free, [
'EUR', 'USD', 'JPY', 'CHF'
], $cache);
$converter = new CurrencyConverter($service);
// ...
Upgrade#
1.x to 2.x#
An extra parameter,
$multiconversionwas added to the constructor. If you are not using named parameters, you need to account for that.