Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@

All notable changes to this project will be documented in this file.

## [3.0.0] - 2026-02-19

### Added
- New `ohlc()` endpoint for OHLC (Open, High, Low, Close) candlestick data (requires paid plan).
- New `setCurrency()` method to set the quote currency for the OHLC endpoint.
- New `setInterval()` method to set the candle interval for the OHLC endpoint (`5m`, `15m`, `30m`, `1h`, `4h`, `12h`, `1d`).

### Changed
- Upgraded to API v2 (`/api/v2/`).

### Removed
- `setLimit()` method and `limit` parameter — API v2 no longer supports filtering currencies by a limit. All supported currencies are always returned (with `null` for unavailable ones).

## [2.0.0] - 2026-02-13

### Changed
Expand Down
82 changes: 71 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
# CurrencyApi PHP wrapper
# CurrencyApi PHP wrapper

[![Latest Stable Version](https://poser.pugx.org/houseofapis/currencyapi/v/stable)](https://packagist.org/packages/houseofapis/currencyapi) [![License](https://poser.pugx.org/houseofapis/currencyapi/license)](https://packagist.org/packages/houseofapis/currencyapi) [![Build Status](https://travis-ci.org/houseofapis/currencyapi-php.svg?branch=master)](https://travis-ci.org/houseofapis/currencyapi-php) [![Coverage Status](https://coveralls.io/repos/github/houseofapis/currencyapi-php/badge.svg?branch=master)](https://coveralls.io/github/houseofapis/currencyapi-php?branch=master)
[![Latest Stable Version](https://poser.pugx.org/houseofapis/currencyapi/v/stable)](https://packagist.org/packages/houseofapis/currencyapi) [![License](https://poser.pugx.org/houseofapis/currencyapi/license)](https://packagist.org/packages/houseofapis/currencyapi) [![Build Status](https://travis-ci.org/houseofapis/currencyapi-php.svg?branch=master)](https://travis-ci.org/houseofapis/currencyapi-php) [![Coverage Status](https://coveralls.io/repos/github/houseofapis/currencyapi-php/badge.svg?branch=master)](https://coveralls.io/github/houseofapis/currencyapi-php?branch=master)

<a href="https://currencyapi.net" title="CurrencyApi">CurrencyApi.net</a> provides live currency rates via a REST API. A live currency feed for over 152 currencies, including physical (USD, GBP, EUR + more) and cryptos (Bitcoin, Litecoin, Ethereum + more). A JSON and XML currency api updated every 60 seconds.
<a href="https://currencyapi.net" title="CurrencyApi">CurrencyApi.net</a> provides live currency rates via a REST API. A live currency feed for over 163 currencies, including physical (USD, GBP, EUR + more) and cryptos (Bitcoin, Litecoin, Ethereum + more). A JSON and XML currency api updated every 60 seconds.

Features:

- Live exchange rates (updated every 60 seconds).
- 152 currencies world currencies.
- 163+ world currencies.
- Popular cryptocurrencies included; Bitcoin, Litecoin etc.
- Convert currencies on the fly with the convert endpoint.
- Historical currency rates back to year 2000.
- OHLC (candlestick) data for technical analysis.
- Easy to follow <a href="https://currencyapi.net/documentation" title="currency-api-documentation">documentation</a>

Signup for a free or paid account <a href="https://currencyapi.net/pricing" title="currency-api-pricing">here</a>.

> **Note:** API v1 is deprecated and will be retired on **31st July 2026**, at which point all v1 traffic will be redirected to v2. This SDK (v3.0.0+) targets API v2. If you are on an older version of this SDK, please upgrade.

## This package is a:

PHP wrapper for <a href="https://currencyapi.net" title="CurrencyApi">CurrencyApi.net</a> endpoints.
Expand Down Expand Up @@ -61,6 +64,8 @@ $currencyApi = new \CurrencyApi\CurrencyApi('API_KEY');

### Live rates:

Returns the latest exchange rates for all supported currencies.

```php
$result = $currencyApi->rates();
```
Expand All @@ -70,7 +75,6 @@ Example with all available methods:
$result = $currencyApi
->setBase('USD')
->setOutput('JSON')
->setLimit('BTC,EUR,GBP')
->rates();
```
**Available methods for rates endpoint**
Expand All @@ -79,12 +83,13 @@ $result = $currencyApi
| --- | --- |
| `setBase()` | The base currency you wish you receive the currency conversions for. This will output all currency conversions for that currency. **Default: USD**. |
| `setOutput()` | Response output in either JSON or XML. **Default: JSON**. |
| `setLimit()` | Limit which currency conversions are returned using the limit param. Comma separated (no space) values. **Optional** |

<br>

### List of available currencies:

Returns a list of all currencies supported by the API.

```php
$result = $currencyApi->currencies();
```
Expand All @@ -106,6 +111,8 @@ $result = $currencyApi

### Convert:

Converts an amount from one currency to another using the latest rates.

```php
$result = $currencyApi
->setAmount(100)
Expand All @@ -127,6 +134,8 @@ $result = $currencyApi

### Historical:

Returns exchange rates for all currencies on a specific date.

```php
$result = $currencyApi->setDate('2019-01-01')->historical();
```
Expand All @@ -137,7 +146,6 @@ Example with all available methods:
$result = $currencyApi
->setDate('2019-01-01')
->setBase('GBP')
->setLimit('USD')
->setOutput('JSON')
->historical();
```
Expand All @@ -149,14 +157,15 @@ $result = $currencyApi
| `setDate()` | The historical date you wish to receive the currency conversions for. This should be formatted as YYYY-MM-DD. **Required**. |
| `setBase()` | The base currency you wish you receive the currency conversions for. This will output all currency conversions for that currency. **Default: USD**. |
| `setOutput()` | Response output in either JSON or XML. **Default: JSON**. |
| `setLimit()` | Limit which currency conversions are returned using the limit param. Comma separated (no space) values. **Optional** |

<br>

### Timeframe:

Returns exchange rates for all currencies across a date range, with one entry per day.

```php
$result = $currencyApi->setStartDate('2019-01-01')->setEndDate('2019-01-05')->historical();
$result = $currencyApi->setStartDate('2019-01-01')->setEndDate('2019-01-05')->timeframe();
```

Example with all available methods:
Expand All @@ -166,7 +175,6 @@ $result = $currencyApi
->setStartDate('2019-01-01')
->setEndDate('2019-01-05')
->setBase('GBP')
->setLimit('USD,BTC')
->setOutput('XML')
->timeframe();
```
Expand All @@ -179,5 +187,57 @@ $result = $currencyApi
| `setEndDate()` | The historical date you wish to receive the currency conversions until. This should be formatted as YYYY-MM-DD. **Required**. |
| `setBase()` | The base currency you wish you receive the currency conversions for. This will output all currency conversions for that currency. **Default: USD**. |
| `setOutput()` | Response output in either JSON or XML. **Default: JSON**. |
| `setLimit()` | Limit which currency conversions are returned using the limit param. Comma separated (no space) values. **Optional** |

<br>

### OHLC (Open, High, Low, Close):

Returns candlestick data for a currency pair on a given date, useful for charting and technical analysis.

```php
$result = $currencyApi
->setCurrency('EUR')
->setDate('2023-12-25')
->ohlc();
```

Example with all available methods:

```php
$result = $currencyApi
->setCurrency('EUR')
->setDate('2023-12-25')
->setBase('GBP')
->setInterval('1h')
->ohlc();
```

**Available methods for OHLC endpoint**

| Methods | Description |
| --- | --- |
| `setCurrency()` | The currency to retrieve OHLC data for. Three letter ISO 4217 currency code. **Required**. |
| `setDate()` | The date to retrieve OHLC data for. This should be formatted as YYYY-MM-DD. **Required**. |
| `setBase()` | The base currency for the rates. **Default: USD**. |
| `setInterval()` | The interval for the OHLC candles. One of: `5m`, `15m`, `30m`, `1h`, `4h`, `12h`, `1d`. **Default: 1d**. |

**Example response:**

```json
{
"valid": true,
"base": "USD",
"quote": "EUR",
"date": "2023-12-25",
"interval": "1d",
"ohlc": [
{
"start": "2023-12-25T00:00:00Z",
"open": 0.9123,
"high": 0.9187,
"low": 0.9098,
"close": 0.9154
}
]
}
```
120 changes: 84 additions & 36 deletions src/CurrencyApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class CurrencyApi
/**
* Version of the API
*/
const API_VERSION = 'v1';
const API_VERSION = 'v2';

/**
* Default base currency
Expand Down Expand Up @@ -53,15 +53,6 @@ class CurrencyApi
*/
protected $output = self::DEFAULT_OUTPUT;

/**
* Limit which currency conversions are returned
* Comma separated values
* eg: 'USD,GBP,BTC'
*
* @var string
*/
protected $limit = '';

/**
* The value of the currency you want to convert from.
* This should be a number and can contain a decimal place.
Expand Down Expand Up @@ -110,6 +101,22 @@ class CurrencyApi
*/
protected $end_date = '';

/**
* The currency to retrieve OHLC data for.
* This will be a three letter ISO 4217 currency code.
*
* @var string
*/
protected $currency = '';

/**
* The interval for OHLC data.
* One of: 5m, 15m, 30m, 1h, 4h, 12h, 1d
*
* @var string
*/
protected $interval = '';

/**
* CurrencyApi constructor.
* @param string $key
Expand Down Expand Up @@ -145,19 +152,6 @@ public function setOutput(string $output) : CurrencyApi
return $this;
}

/**
* Sets the limit of currencies returned
* See limit property for description ^
*
* @param string $limit
* @return CurrencyApi
*/
public function setLimit(string $limit) : CurrencyApi
{
$this->limit = str_replace(' ', '', strtoupper($limit));
return $this;
}

/**
* Sets the amount to convert
* See amount property for description ^
Expand Down Expand Up @@ -236,6 +230,33 @@ public function setEndDate(string $end_date) : CurrencyApi
return $this;
}

/**
* Sets the currency for the OHLC endpoint
* See currency property for description ^
*
* @param string $currency
* @return CurrencyApi
*/
public function setCurrency(string $currency) : CurrencyApi
{
$this->currency = strtoupper($currency);
return $this;
}

/**
* Sets the interval for the OHLC endpoint
* See interval property for description ^
* One of: 5m, 15m, 30m, 1h, 4h, 12h, 1d
*
* @param string $interval
* @return CurrencyApi
*/
public function setInterval(string $interval) : CurrencyApi
{
$this->interval = $interval;
return $this;
}

/**
* Get the live rates
*
Expand All @@ -245,7 +266,7 @@ public function setEndDate(string $end_date) : CurrencyApi
*/
public function rates() : array
{
return $this->get($this->buildUrl('rates', $this->getCommonBaseAndLimit()));
return $this->get($this->buildUrl('rates', $this->getCommonBase()));
}

/**
Expand Down Expand Up @@ -296,6 +317,18 @@ public function timeframe() : array
return $this->get($this->buildUrl('timeframe', $this->getTimeFrameParams()));
}

/**
* Get OHLC (Open, High, Low, Close) data for a currency pair
*
* @return array
* @throws BadRequestException
* @throws ConnectException
*/
public function ohlc() : array
{
return $this->get($this->buildUrl('ohlc', $this->getOhlcParams()));
}

/**
* Prepares parameters for the convert endpoint
*
Expand All @@ -317,7 +350,7 @@ protected function getConvertParams() : array
*/
protected function getHistoricalParams() : array
{
return array_merge(['date' => $this->date], $this->getCommonBaseAndLimit());
return array_merge(['date' => $this->date], $this->getCommonBase());
}

/**
Expand All @@ -330,25 +363,40 @@ protected function getTimeFrameParams() : array
return array_merge([
'start_date' => $this->start_date,
'end_date' => $this->end_date
], $this->getCommonBaseAndLimit());
], $this->getCommonBase());
}

/**
* Adds commonly used base and limit parameters if they are set
* Prepares parameters for the OHLC endpoint
*
* @return array
*/
protected function getCommonBaseAndLimit() : array
protected function getOhlcParams() : array
{
$queryParams = [];

if($this->base !== self::DEFAULT_BASE) {
$queryParams['base'] = $this->base;
$params = [
'currency' => $this->currency,
'date' => $this->date,
];
if ($this->base !== self::DEFAULT_BASE) {
$params['base'] = $this->base;
}
if (!empty($this->interval)) {
$params['interval'] = $this->interval;
}
if(!empty($this->limit)) {
$queryParams['limit'] = $this->limit;
return $params;
}

/**
* Adds the base parameter if it differs from the default
*
* @return array
*/
protected function getCommonBase() : array
{
if ($this->base !== self::DEFAULT_BASE) {
return ['base' => $this->base];
}
return $queryParams;
return [];
}

/**
Expand Down Expand Up @@ -387,7 +435,7 @@ protected function buildUrl(string $endpoint, array $urlParams = []) : string
* Example of what's returned:
* [
* [valid] => true
* [timestamp] => 1583182512
* [updated] => 1583182512
* [base] => GBP
* [rates] => [
* [AED] => 4.68987
Expand Down
Loading