1. Go to this page and download the library: Download centralbank/exchangerates library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
use Centralbank\Exchangerates\Services\ExchangeRateService;
class YourController extends Controller
{
protected $exchangeRateService;
public function __construct(ExchangeRateService $exchangeRateService)
{
$this->exchangeRateService = $exchangeRateService;
}
public function getRates()
{
// Get all rates
$allRates = $this->exchangeRateService->getAllRates();
// Get specific currency
$usdRate = $this->exchangeRateService->getRate('USD');
// Get multiple currencies
$rates = $this->exchangeRateService->getRates(['USD', 'EUR', 'GBP']);
// Refresh cache
$freshRates = $this->exchangeRateService->refreshCache();
return response()->json($allRates);
}
}
use Centralbank\Exchangerates\Services\ExchangeRateService;
class UpdateExchangeRatesCommand extends Command
{
protected $signature = 'exchange:update';
public function handle(ExchangeRateService $exchangeRateService)
{
$rates = $exchangeRateService->refreshCache();
if ($rates) {
$this->info('Exchange rates updated successfully!');
} else {
$this->error('Failed to update exchange rates.');
}
}
}