How to Get Currency Exchange Rates, Convert Price Values in PHP

Cloudmersive
2 min readMar 15, 2023

--

Building powerful currency services into your application is a simpler process than it might seem. Just use the PHP code examples provided below to structure requests to our free-to-use Currency API — you’ll be able to retrieve up-to-date exchange rate data and convert prices instantly.

To get started, run this command to install the SDK:

composer require cloudmersive/cloudmersive_currency_api_client

Next, grab a free-tier API key by registering a free account on our website — you’ll need this to authenticate each request (free accounts provide a limit of 800 API calls per month & no commitments upon reaching that limit).

With that out of the way, you can now use the following code to request our list of available currencies and their corresponding countries. This provides each available currency’s ISO code, symbol, name (in English), and more:

<?php
require_once(__DIR__ . '/vendor/autoload.php');

// Configure API key authorization: Apikey
$config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKey('Apikey', 'YOUR_API_KEY');



$apiInstance = new Swagger\Client\Api\CurrencyExchangeApi(


new GuzzleHttp\Client(),
$config
);

try {
$result = $apiInstance->currencyExchangeGetAvailableCurrencies();
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling CurrencyExchangeApi->currencyExchangeGetAvailableCurrencies: ', $e->getMessage(), PHP_EOL;
}
?>

To get up-to-date exchange rates between currencies on that list, use the code examples below and provide the three-digit ISO value (e.g., USD, EUR, etc.) for the source and destination currencies in your request:

<?php
require_once(__DIR__ . '/vendor/autoload.php');

// Configure API key authorization: Apikey
$config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKey('Apikey', 'YOUR_API_KEY');



$apiInstance = new Swagger\Client\Api\CurrencyExchangeApi(


new GuzzleHttp\Client(),
$config
);
$source = "source_example"; // string | Source currency three-digit code (ISO 4217), e.g. USD, EUR, etc.
$destination = "destination_example"; // string | Destination currency three-digit code (ISO 4217), e.g. USD, EUR, etc.

try {
$result = $apiInstance->currencyExchangeGetExchangeRate($source, $destination);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling CurrencyExchangeApi->currencyExchangeGetExchangeRate: ', $e->getMessage(), PHP_EOL;
}
?>

Finally, use the following code examples to convert specific price values from one currency to another, once again including the ISO codes for the source and destination currencies in your request. In addition to that, of course, you’ll need to specify the source price for the conversion:

<?php
require_once(__DIR__ . '/vendor/autoload.php');

// Configure API key authorization: Apikey
$config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKey('Apikey', 'YOUR_API_KEY');



$apiInstance = new Swagger\Client\Api\CurrencyExchangeApi(


new GuzzleHttp\Client(),
$config
);
$source = "source_example"; // string | Source currency three-digit code (ISO 4217), e.g. USD, EUR, etc.
$destination = "destination_example"; // string | Destination currency three-digit code (ISO 4217), e.g. USD, EUR, etc.
$source_price = 1.2; // double | Input price, such as 19.99 in source currency

try {
$result = $apiInstance->currencyExchangeConvertCurrency($source, $destination, $source_price);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling CurrencyExchangeApi->currencyExchangeConvertCurrency: ', $e->getMessage(), PHP_EOL;
}
?>

And just like that, you can put your currency exchange needs to rest. Easy!

--

--

Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.