Laravel Exchange Rate service.
Require this package with composer via command:
composer require yoelpc4/laravel-exchange-rate
Available exchange rate service providers:
If you're planning to use Free Currency Converter Api
as a provider. get your api key here.
Then add these lines to your .env.
EXCHANGE_RATE_PROVIDER=free_currency_converter_api
FREE_CURRENCY_CONVERTER_API_BASE_URL=https://free.currconv.com/api/v8/
FREE_CURRENCY_CONVERTER_API_KEY=
Publish package configuration via command:
php artisan vendor:publish --provider="Yoelpc4\LaravelExchangeRate\ExchangeRateServiceProvider" --tag=config
Publish package resources via command:
php artisan vendor:publish --provider="Yoelpc4\LaravelExchangeRate\ExchangeRateServiceProvider" --tag=resources
Get supported currencies
try {
$supportedCurrencies = \ExchangeRateService::currencies();
} catch (\Psr\Http\Client\ClientExceptionInterface $e) {
throw $e;
}
The return value is an instance of \Yoelpc4\LaravelExchangeRate\Contracts\SupportedCurrenciesInterface
object.
Get latest exchange rate
try {
$base = 'IDR';
$targets = [
'USD', 'DZD',
];
$latestExchangeRate = \ExchangeRateService::latest($base, $targets);
} catch (\Illuminate\Validation\ValidationException $e) {
throw $e;
} catch (\Psr\Http\Client\ClientExceptionInterface $e) {
throw $e;
}
The return value is an instance of \Yoelpc4\LaravelExchangeRate\Contracts\LatestExchangeRateInterface
object.
Get historical exchange rate
try {
$base = 'IDR';
$targets = [
'USD', 'DZD',
];
$date = now()->subDays(3)->toDateString();
$historicalExchangeRate = \ExchangeRateService::historical($base, $targets, $date);
} catch (\Illuminate\Validation\ValidationException $e) {
throw $e;
} catch (\Psr\Http\Client\ClientExceptionInterface $e) {
throw $e;
}
The return value is an instance of \Yoelpc4\LaravelExchangeRate\Contracts\HistoricalExchangeRateInterface
object.
Get time series exchange rate
try {
$base = 'IDR';
$targets = [
'USD', 'DZD',
];
$startDate = now()->subDays(8)->toDateString();
$endDate = now()->toDateString();
$timeSeriesExchangeRate = \ExchangeRateService::timeSeries($base, $targets, $startDate, $endDate);
} catch (\Illuminate\Validation\ValidationException $e) {
throw $e;
} catch (\Psr\Http\Client\ClientExceptionInterface $e) {
throw $e;
}
The return value is an instance of \Yoelpc4\LaravelExchangeRate\Contracts\TimeSeriesExchangeRateInterface
object.
Switch between available providers
$exchangeRateService = \ExchangeRateService::provider('free_currency_converter_api');
This package will run validation based on respective provider rules before dispatching some requests,
therefore it will throw \Illuminate\Validation\ValidationException
for every unmet validation rules.
The Laravel Exchange Rate is open-sourced software licensed under the MIT license.