Table of Contents
Live Currency Rates Using Our PHP SDK
Ready to enhance your PHP projects? Our PHP SDK for CurrencyApi.net is designed to simplify your integration process. Whether you're building web applications, data analysis tools, or server-side solutions, our SDK provides the tools you need to seamlessly incorporate live currency rates into your PHP projects. Stick with us, and you'll be leveraging real-time currency data like a pro!
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:
composer require houseofapis/currencyapi
Installing the PHP SDK
After installation, include the SDK in your PHP project. There are two ways to do this, via composer or manually.
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:
<?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 Rates in PHP
Let's dive into fetching live currency rates using the following method:
$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.
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 PHP SDK
Easily convert amounts between currencies using our Currency API and PHP SDK. This method provides a streamlined response, optimizing performance and reducing data overhead.
The convert()
method utilizes the /convert
endpoint of our Currency API.
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.
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.
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:
- 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 PHP 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. With easy setup, comprehensive documentation, and dependable support, enhancing your projects with up-to-date currency data has never been easier.
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.
Meet the Author
