PHP code example of xratesapi / php-sdk

1. Go to this page and download the library: Download xratesapi/php-sdk 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/ */

    

xratesapi / php-sdk example snippets


use XRatesApi\Client;

$client = new Client('YOUR_API_KEY');

// Latest rates: rates[T] = "X T per 1 base"
$response = $client->latest('USD', ['EUR', 'GBP']);
// ['base' => 'USD', 'rates' => ['EUR' => 0.864, 'GBP' => 0.79], ...]

// Historical
$response = $client->historical('2026-05-01', 'USD', ['EUR']);

// Convert an amount
$response = $client->convert('USD', 'EUR', 100.0);
// ['from' => 'USD', 'to' => 'EUR', 'amount' => 100, 'result' => 86.4]

// Time-series
$response = $client->timeseries('2026-01-01', '2026-01-31', 'USD', ['EUR']);

// Rate fluctuation
$response = $client->fluctuation('2026-01-01', '2026-01-31', 'USD', ['EUR']);

// Currencies
$response = $client->currencies();

// Public status (no API key 

use XRatesApi\Exceptions\RateLimitException;
use XRatesApi\Exceptions\ApiException;

try {
    $rates = $client->latest('USD', ['EUR']);
} catch (RateLimitException $e) {
    // back off and retry
} catch (ApiException $e) {
    // log and surface to caller
    error_log("XRates: " . $e->getMessage());
}

$guzzle = new \GuzzleHttp\Client([
    'timeout' => 5,
    'connect_timeout' => 2,
]);

$client = new Client('YOUR_API_KEY', $guzzle);

$client = new Client('YOUR_API_KEY', null, 'https://staging.xratesapi.com');
bash
composer