PHP

PHP Exchange Rate API

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.

Quick Start

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
Get your free API keySign up here to get an API key that starts with era_live_. The free plan includes 300 requests per month.

Using the SDK

The exchangerateapi/sdk Composer package wraps every API endpoint and handles authentication, retries, and response parsing for you.

Get Exchange Rate

<?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 an Amount

// Convert 1000 USD to EUR
$result = $client->convert('USD', 'EUR', 1000);
echo "$1,000 = {$result['result']} EUR";

Historical Rates

// Get 30-day historical data
$history = $client->getHistoricalRates('USD', 'EUR', '30d');
foreach ($history['data'] as $point) {
    echo "{$point['date']}: {$point['rate']}\n";
}

List Supported Currencies

$symbols = $client->getSymbols();
foreach ($symbols['currencies'] as $currency) {
    echo "{$currency['code']}: {$currency['name']}\n";
}

Without SDK (Using file_get_contents)

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

Using cURL

<?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.";
}

Response Format

Single Target Response

[
  {
    "rate": 0.9215,
    "source": "USD",
    "target": "EUR",
    "time": "2026-05-15T12:00:00Z"
  }
]

Multiple Targets Response

[
  { "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" }
]
PHP 8.0+ recommended — The SDK requires PHP 8.0 or later. If you are using an older version of PHP, use the file_get_contents or cURL examples above.

Start building with PHP

Get your free API key and start fetching exchange rates in PHP in under a minute.

Get Free API Key →