Table of Contents
Live Exchange Rates in PHP – PHP Currency API SDK
Written by Oli Girling • Last updated: 9th October 2025 • Read time: 4–8 minutes Need live exchange rates in PHP? We've got you covered.
Currency API provides simple and reliable APIs for fetching real‑time data, building a currency converter in PHP using API calls, and powering dashboards with fresh rates.
This guide shows how to use our currency api for php with our PHP SDK - getting live exchange rates in PHP in minutes.
Getting Started with the PHP SDK
To get started with the CurrencyApi.net PHP SDK, ensure you have PHP 7.1 or higher installed. You can install the SDK using Composer and be ready to call the exchange rates api php endpoints in a few seconds:
composer require houseofapis/currencyapiInstalling the PHP SDK
After installation, include the SDK in your PHP project. There are two ways to do this, via composer or manually — either way, you’ll be ready to use a php currency api quickly.
Using Composer
Composer is a dependency manager for PHP. It allows you to declare the libraries your project depends on and it will install them in your project for you.
If you have installed via composer, be sure you have required the autoloader in your project.
Then use the HouseOfApisCurrencyApiCurrencyApi class in your PHP project.
use HouseOfApis\CurrencyApi\CurrencyApi;Manual Installation Without Composer
require_once('/path/to/currencyapi/src/CurrencyApi.php');Creating Your PHP SDK Instance
You're now ready to start using the CurrencyApi.net PHP SDK. Begin by creating an instance of the CurrencyApi class with your API key — the fastest way to wire up a production‑ready currency api php client:
<?php
use HouseOfApisCurrencyApiCurrencyApi;
$currencyApi = new CurrencyApi('YOUR_API_KEY');Not signed up yet?
Create your free account and get started with our PHP SDK in minutes.
Retrieving Live Exchange Rates in PHP
Let's dive into fetching live currency rates using the following method from our php exchange rates api:
$result = $currencyApi->rates();This will return a JSON response containing all the current currency rates.
The rates() method is a convenient way to access the /rates endpoint.
Here's a complete example showcasing the API response:
$currencyApi = new CurrencyApi('YOUR_API_KEY');
try {
$result = $currencyApi->rates();
print_r($result);
} catch (Exception $e) {
print_r("An error occurred: " . $e->getMessage());
}Great! You've successfully retrieved live currency rates using our PHP SDK — a clean example of calling the exchange rates api php endpoints.
Available Methods for Rates Endpoint with PHP SDK
| Method | Description |
|---|---|
setBase() | Sets the base currency for conversions. Outputs all currency conversions for the specified currency. Default is USD. |
setOutput() | Specifies the response format: JSON or XML. Default is JSON. |
setLimit() | Limits which currency conversions are returned using the limit parameter. Comma-separated (no space) values. Optional. |
Currency Conversion with the PHP Currency API SDK
Easily convert amounts between currencies using our Currency API and PHP SDK — a straightforward php currency converter api workflow.
The convert() method utilizes the /convert endpoint of our Currency API, making it a practical currency converter api php example.
Let's convert 100 USD to GBP with the PHP SDK:
$currencyApi = new CurrencyApi('YOUR_API_KEY');
try {
$result = $currencyApi
->setAmount(100)
->setFrom('USD')
->setTo('GBP')
->convert();
print_r($result);
} catch (Exception $e) {
print_r("An error occurred: " . $e->getMessage());
}As shown, the response is concise and contains only the necessary conversion details.
Available PHP SDK Methods for Convert Endpoint
| Method | Description |
|---|---|
setAmount() | The value of the currency you want to convert from. This should be a number and can contain a decimal place. Required. |
setFrom() | The currency you want to convert from. Must be a three-letter ISO 4217 currency code. Required. |
setTo() | The currency you want to convert to. Must be a three-letter ISO 4217 currency code. Required. |
setOutput() | Specifies the response format: JSON or XML. Default is JSON. |
Accessing Historical Currency Rates in PHP
Need historical currency data? Our SDK provides endpoints to access past currency rates with ease — ideal for any currency exchange api php use case.
The historical() method leverages the /historical endpoint of our Currency API.
Retrieve historical rates for USD on January 1st, 2019 using the PHP SDK:
$currencyApi = new CurrencyApi('YOUR_API_KEY');
try {
$result = $currencyApi
->setDate('2019-01-01')
->setBase('USD')
->setOutput('JSON')
->historical();
print_r($result);
} catch (Exception $e) {
print_r("An error occurred: " . $e->getMessage());
}The response provides an array with historical currency rates for the specified date.
Available Methods for Historical Endpoint
| Method | Description |
|---|---|
setDate() | The historical date you wish to receive the currency conversions for. Format: YYYY-MM-DD. Required. |
setBase() | Sets the base currency for conversions. Outputs all currency conversions for the specified currency. Default is USD. |
setOutput() | Specifies the response format: JSON or XML. Default is JSON. |
setLimit() | Limits which currency conversions are returned using the limit parameter. Comma-separated (no space) values. Optional. |
Fetching Rates Over a Timeframe with PHP
Accessing currency rates over a specific timeframe enables developers to build applications that monitor currency fluctuations over time, such as financial dashboards, analytics tools, and more.
The timeframe() method utilizes the /timeframe endpoint of our Currency API — a common pattern in exchange rates api php integrations.
Retrieve rates for GBP between January 1st, 2019 and January 5th, 2019 using the PHP SDK:
$currencyApi = new CurrencyApi('YOUR_API_KEY');
try {
$result = $currencyApi
->setStartDate('2019-01-01')
->setEndDate('2019-01-05')
->setBase('GBP')
->setLimit('USD,BTC')
->setOutput('XML')
->timeframe();
print_r($result);
} catch (Exception $e) {
print_r("An error occurred: " . $e->getMessage());
}The response provides an array with currency rates over the specified timeframe, simplifying the process of tracking currency trends.
Available Methods for Timeframe Endpoint
| Method | Description |
|---|---|
setStartDate() | The start date for the currency rate timeframe. Format: YYYY-MM-DD. Required. |
setEndDate() | The end date for the currency rate timeframe. Format: YYYY-MM-DD. Required. |
setBase() | Sets the base currency for conversions. Outputs all currency conversions for the specified currency. Default is USD. |
setOutput() | Specifies the response format: JSON or XML. Default is JSON. |
setLimit() | Limits which currency conversions are returned using the limit parameter. Comma-separated (no space) values. Optional. |
Best Practices for PHP Integration
To ensure a smooth and efficient integration with the PHP SDK, adhere to the following best practices — especially helpful when building a php currency converter api or broader currency conversion api php workflow:
- Cache API responses when appropriate to minimize redundant API calls and enhance performance.
- Implement robust error handling to gracefully manage unexpected issues and maintain application stability.
- Securely store your API key using environment variables or configuration files that are excluded from version control.
- Regularly consult the official documentation for updates and advanced usage techniques.
Error Handling in the PHP Currency API SDK
Effective error handling ensures your application remains resilient and provides meaningful feedback to users:
$currencyApi = new CurrencyApi('YOUR_API_KEY');
try {
$result = $currencyApi->rates();
print_r($result);
} catch (Exception $e) {
print_r("An error occurred: " . $e->getMessage());
}Conclusion
The CurrencyApi.net PHP SDK offers a straightforward and efficient method to integrate live currency rates into your PHP applications. Whether you need a free currency converter api php starting point or a robust currency api php for production, you can build fast, reliable features with our exchange rates api php.
Other Useful Links:
Explore Other SDKs
Not working with PHP? Explore our other SDKs tailored for different programming languages:
Python SDK
Effortlessly incorporate live currency data into your Python applications with our intuitive SDK. Access real-time exchange rates, currency conversions, and more.
Explore the Python SDK →GoLang SDK
Quickly implement currency data into your GoLang applications with our user-friendly SDK. Access live exchange rates, currency conversions, and more.
Explore the GoLang SDK →NodeJS SDK
Integrate real-time currency services into your NodeJS projects. Our SDK provides you with powerful tools for handling currency data with minimal effort.
Discover the NodeJS SDK →Get coding with PHP today!
Integrate live currency rates into your PHP applications with our PHP SDK.