How do I get exchange rates in PHP? #3
Answered
by
cahthuranag
cahthuranag
asked this question in
Q&A
-
|
What's the simplest way to fetch live exchange rates in PHP? AnswerInstall the official PHP SDK via Composer: composer require exchangerateapi/sdkUsage: use ExchangeRateAPI\ExchangeRateAPI;
$client = new ExchangeRateAPI('era_live_YOUR_KEY');
// Get exchange rate
$rate = $client->getRate('USD', 'EUR');
echo "1 USD = {$rate[0]['rate']} EUR\n";
// Convert amount
$result = $client->convert('USD', 'EUR', 100);
echo "\$100 = €{$result['result']}\n";
// Get historical rates
$history = $client->getHistoricalRates('USD', 'EUR', '30d');
foreach ($history['rates'] as $point) {
echo "{$point['time']}: {$point['rate']}\n";
}Requires only PHP 7.4+ with Get your free API key: exchange-rateapi.com/register |
Beta Was this translation helpful? Give feedback.
Answered by
cahthuranag
Jun 21, 2026
Replies: 1 comment
-
|
Install the official PHP SDK via Composer: composer require exchangerateapi/sdkUsage: use ExchangeRateAPI\ExchangeRateAPI;
$client = new ExchangeRateAPI('era_live_YOUR_KEY');
// Get exchange rate
$rate = $client->getRate('USD', 'EUR');
echo "1 USD = {$rate[0]['rate']} EUR\n";
// Convert amount
$result = $client->convert('USD', 'EUR', 100);
echo "\$100 = €{$result['result']}\n";
// Get historical rates
$history = $client->getHistoricalRates('USD', 'EUR', '30d');
foreach ($history['rates'] as $point) {
echo "{$point['time']}: {$point['rate']}\n";
}Requires only PHP 7.4+ with Get your free API key: exchange-rateapi.com/register |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
cahthuranag
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Install the official PHP SDK via Composer:
Usage:
Requires only PHP 7.4+ with
ext-curlandext-json(both ship with standard PHP). No external Composer dependencies.Get your free API k…