Fetch live and historical exchange rates in PHP. Install our SDK with Composer or call the REST API directly with file_get_contents or cURL.
Install the official PHP SDK and fetch your first exchange rate in under a minute.
composer require exchangerateapi/sdk
<?php
use ExchangeRateAPI\ExchangeRateAPI;
$client = new ExchangeRateAPI('era_live_YOUR_API_KEY');
// Get the latest USD to EUR rate
$rate = $client->getRate('USD', 'EUR');
echo "1 USD = {$rate['rate']} EUR";
// Output: 1 USD = 0.9215 EUR
era_live_. The free plan includes 300 requests per month.
The exchangerateapi/sdk Composer package wraps every API endpoint and handles authentication, retries, and response parsing for you.
<?php
use ExchangeRateAPI\ExchangeRateAPI;
$client = new ExchangeRateAPI('era_live_YOUR_API_KEY');
// Single currency pair
$rate = $client->getRate('USD', 'EUR');
echo "1 USD = {$rate['rate']} EUR";
// Convert 1000 USD to EUR
$result = $client->convert('USD', 'EUR', 1000);
echo "$1,000 = {$result['result']} EUR";
// Get 30-day historical data
$history = $client->getHistoricalRates('USD', 'EUR', '30d');
foreach ($history['data'] as $point) {
echo "{$point['date']}: {$point['rate']}\n";
}
$symbols = $client->getSymbols();
foreach ($symbols['currencies'] as $currency) {
echo "{$currency['code']}: {$currency['name']}\n";
}
If you prefer to call the API directly without the SDK, use PHP's built-in file_get_contents with a stream context for the authorization header.
<?php
$opts = [
'http' => [
'header' => "Authorization: Bearer era_live_YOUR_API_KEY\r\n"
]
];
$context = stream_context_create($opts);
$response = file_get_contents(
'https://exchange-rateapi.com/api/v1/rates?source=USD&target=EUR',
false,
$context
);
$data = json_decode($response, true);
echo "1 USD = " . $data[0]['rate'] . " EUR";
// Output: 1 USD = 0.9215 EUR
<?php
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => 'https://exchange-rateapi.com/api/v1/rates?source=USD&target=EUR,GBP,JPY',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => [
'Authorization: Bearer era_live_YOUR_API_KEY'
],
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode === 200) {
$rates = json_decode($response, true);
foreach ($rates as $rate) {
echo "1 USD = {$rate['rate']} {$rate['target']}\n";
}
} elseif ($httpCode === 401) {
echo "Invalid API key. Check your Authorization header.";
} elseif ($httpCode === 429) {
echo "Rate limit exceeded. Try again later.";
}
[
{
"rate": 0.9215,
"source": "USD",
"target": "EUR",
"time": "2026-05-15T12:00:00Z"
}
]
[
{ "rate": 0.9215, "source": "USD", "target": "EUR", "time": "2026-05-15T12:00:00Z" },
{ "rate": 0.7891, "source": "USD", "target": "GBP", "time": "2026-05-15T12:00:00Z" },
{ "rate": 151.42, "source": "USD", "target": "JPY", "time": "2026-05-15T12:00:00Z" }
]
file_get_contents or cURL examples above.
Get your free API key and start fetching exchange rates in PHP in under a minute.
Get Free API Key →