PHPackages                             xdemonme/currency - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. xdemonme/currency

ActiveLibrary

xdemonme/currency
=================

A PHP library for current and historical currency exchange rates &amp; crypto exchange rates based on the free API provided by exchangerate.host

v1.1.0(2y ago)01271MITPHPPHP &gt;=7.2

Since Oct 20Pushed 2y agoCompare

[ Source](https://github.com/xDemonME/currency)[ Packagist](https://packagist.org/packages/xdemonme/currency)[ Fund](https://ko-fi.com/amrshawky)[ RSS](/packages/xdemonme-currency/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (2)Versions (2)Used By (1)

Currency
========

[](#currency)

[![Tests](https://github.com/amrshawky/currency/workflows/Tests/badge.svg?branch=master)](https://github.com/amrshawky/currency/workflows/Tests/badge.svg?branch=master) [![Packagist License](https://camo.githubusercontent.com/b91a4466525cb0baba452ea999dc4c9f6c5f7abefd9c779e8d3bdb48012ff8b4/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f616d72736861776b792f63757272656e63793f636f6c6f723d73756363657373266c6162656c3d4c6963656e7365)](https://camo.githubusercontent.com/b91a4466525cb0baba452ea999dc4c9f6c5f7abefd9c779e8d3bdb48012ff8b4/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f616d72736861776b792f63757272656e63793f636f6c6f723d73756363657373266c6162656c3d4c6963656e7365) [![Packagist Version](https://camo.githubusercontent.com/43267c1f62afe1b30f138e567be547403c9a25878709e0bb8f4e81251b0ef548/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f616d72736861776b792f63757272656e63793f6c6162656c3d5061636b6167697374)](https://camo.githubusercontent.com/43267c1f62afe1b30f138e567be547403c9a25878709e0bb8f4e81251b0ef548/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f616d72736861776b792f63757272656e63793f6c6162656c3d5061636b6167697374) [![Packagist Downloads](https://camo.githubusercontent.com/0f230de23fd7a511d5f77fe1bf3cd7b6078433e20e2d07cea15a31e9d0cbdce9/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f616d72736861776b792f63757272656e63793f636f6c6f723d73756363657373266c6162656c3d446f776e6c6f616473)](https://camo.githubusercontent.com/0f230de23fd7a511d5f77fe1bf3cd7b6078433e20e2d07cea15a31e9d0cbdce9/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f616d72736861776b792f63757272656e63793f636f6c6f723d73756363657373266c6162656c3d446f776e6c6f616473)

> **This package supports exchangerate.host API keys.**

Currency is a simple PHP library for current and historical currency exchange rates &amp; crypto exchange rates. based on the free API [exchangerate.host](https://exchangerate.host "exchangerate.host Homepage")!

Requirements
------------

[](#requirements)

- PHP &gt;= 7.2
- guzzlehttp &gt;= 6.0

Installation
------------

[](#installation)

```
composer require xdemonme/currency

```

Usage
-----

[](#usage)

### 1. Currency Conversion

[](#1-currency-conversion)

To convert from one currency to another you may chain the methods:

```
require 'vendor/autoload.php';

use AmrShawky\Currency;

$accessKey = 'YOUR_API_ACCESS_KEY';

Currency::convert()
        ->setAccessKey($accessKey)
        ->from('USD')
        ->to('EUR')
        ->get();
```

This will return the converted amount or `null` on failure.

The amount to be converted is default to `1`, you may specify the amount:

```
use AmrShawky\Currency;

$accessKey = 'YOUR_API_ACCESS_KEY';

Currency::convert()
        ->setAccessKey($accessKey)
        ->from('USD')
        ->to('EUR')
        ->amount(50)
        ->get();
```

#### Available Methods

[](#available-methods)

- Convert currency using historical exchange rates `YYYY-MM-DD`:

```
use AmrShawky\Currency;

$accessKey = 'YOUR_API_ACCESS_KEY';

Currency::convert()
        ->setAccessKey($accessKey)
        ->from('USD')
        ->to('EUR')
        ->date('2019-08-01')
        ->get();
```

- Round the converted amount to decimal places:

```
use AmrShawky\Currency;

$accessKey = 'YOUR_API_ACCESS_KEY';

Currency::convert()
        ->setAccessKey($accessKey)
        ->from('USD')
        ->to('EUR')
        ->round(2)
        ->get();
```

- You may also switch data source between forex `default`, bank view or crypto currencies:

```
use AmrShawky\Currency;

$accessKey = 'YOUR_API_ACCESS_KEY';

Currency::convert()
        ->setAccessKey($accessKey)
        ->from('BTC')
        ->to('ETH')
        ->source('crypto')
        ->get();
```

### 2. Live (latest) Rates

[](#2-live-latest-rates)

> Note: This method is DEPRECATED

To get latest rates you may chain the methods:

```
use AmrShawky\Currency;

$accessKey = 'YOUR_API_ACCESS_KEY';

Currency::rates()
        ->setAccessKey($accessKey)
        ->live()
        ->get();

// ['USD' =>  1.215707, ...]

Currency::rates()
        ->setAccessKey($accessKey)
        ->live()
        ->source('crypto')
        ->get();

// ['ETH' => 3398.61, ...]
```

This will return an `array` of all available currencies or `null` on failure.

#### Available Methods

[](#available-methods-1)

- Just like currency conversion you may chain any of the available methods:

```
use AmrShawky\Currency;

$accessKey = 'YOUR_API_ACCESS_KEY';

Currency::rates()
        ->setAccessKey($accessKey)
        ->live()
        ->symbols(['USD', 'EUR', 'EGP']) //An array of currency codes to limit output currencies
        ->base('GBP') //Changing base currency (default: EUR). Enter the three-letter currency code of your preferred base currency.
        ->amount(5.66) //Specify the amount to be converted
        ->round(2) //Round numbers to decimal places
        ->source('ecb') //Switch data source between forex `default`, bank view or crypto currencies.
        ->get();
```

### 3. Historical Rates

[](#3-historical-rates)

Historical rates are available for most currencies all the way back to the year of 1999.

```
use AmrShawky\Currency;

$accessKey = 'YOUR_API_ACCESS_KEY';

Currency::rates()
        ->setAccessKey($accessKey)
        ->historical('2020-01-01') //`YYYY-MM-DD` Required date parameter to get the rates for
        ->get();

// ['USD' =>  1.1185, ...]

Currency::rates()
        ->setAccessKey($accessKey)
        ->historical('2021-03-30')
        ->source('crypto')
        ->get();

// ['BTC' =>  2.0E-5, ...]
```

Same as latest rates you may chain any of the available methods:

```
use AmrShawky\Currency;

$accessKey = 'YOUR_API_ACCESS_KEY';

Currency::rates()
        ->setAccessKey($accessKey)
        ->historical('2020-01-01')
        ->symbols(['USD', 'EUR', 'CZK'])
        ->base('GBP')
        ->amount(5.66)
        ->round(2)
        ->source('ecb')
        ->get();
```

### 4. Timeframe (timeseries) Rates

[](#4-timeframe-timeseries-rates)

Timeframe (timeseries) are for daily historical rates between two dates of your choice, with a maximum time frame of 365 days. This will return an `array` or `null` on failure.

```
use AmrShawky\Currency;

$accessKey = 'YOUR_API_ACCESS_KEY';

Currency::rates()
        ->setAccessKey($accessKey)
        ->timeFrame('2021-05-01', '2021-05-02') //`YYYY-MM-DD` Required dates range parameters
        ->symbols(['USD']) //[optional] An array of currency codes to limit output currencies
        ->base('GBP') //[optional] Changing base currency (default: EUR). Enter the three-letter currency code of your preferred base currency.
        ->amount(5.66) //[optional] Specify the amount to be converted (default: 1)
        ->round(2) //[optional] Round numbers to decimal places
        ->source('ecb') //[optional] Switch data source between forex `default`, bank view or crypto currencies.
        ->get();

/**
[
    '2021-05-01' => [
        "USD" => 1.201995
    ],
    '2021-05-02' => [
        "USD" => 1.2027
    ]
]
 */
```

### 5. Change (fluctuations)

[](#5-change-fluctuations)

Retrieve information about how currencies fluctuate on a day-to-day basis, with a maximum time frame of 365 days. This will return an `array` or `null` on failure.

```
use AmrShawky\Currency;

$accessKey = 'YOUR_API_ACCESS_KEY';

Currency::rates()
        ->setAccessKey($accessKey)
        ->change('2021-03-29', '2021-04-15') //`YYYY-MM-DD` Required dates range parameters
        ->symbols(['USD']) //[optional] An array of currency codes to limit output currencies
        ->base('GBP') //[optional] Changing base currency (default: EUR). Enter the three-letter currency code of your preferred base currency.
        ->amount(5.66) //[optional] Specify the amount to be converted (default: 1)
        ->round(2) //[optional] Round numbers to decimal places
        ->source('ecb') //[optional] Switch data source between forex `default`, bank view or crypto currencies.
        ->get();

/**
 [
    'USD' => [
        "start_rate" => 1.376454,
        "end_rate"   => 1.37816,
        "change"     => -0.001706,
        "change_pct" => -0.001239
        ]
 ]
 */
```

### Throwing Exceptions

[](#throwing-exceptions)

The default behavior is to return `null` for errors that occur during the request *(connection timeout, DNS errors, client or server error status code, missing API success parameter, etc.)*.

If you would like to throw an exception instead, you may use the `throw` method, The `throw` method returns the currency instance, allowing you to chain other methods:

```
use AmrShawky\Currency;

$accessKey = 'YOUR_API_ACCESS_KEY';

Currency::convert()
        ->setAccessKey($accessKey)
        ->from('USD')
        ->to('EUR')
        ->amount(20)
        ->throw()
        ->get();
```

If you would like to perform some additional logic before the exception is thrown, you may pass a closure to the throw method:

```
use AmrShawky\Currency;

$accessKey = 'YOUR_API_ACCESS_KEY';

Currency::convert()
        ->setAccessKey($accessKey)
        ->from('USD')
        ->to('EUR')
        ->amount(20)
        ->throw(function ($response, $e) {
            //
        })
        ->get();
```

### Using HTTPS

[](#using-https)

If your Subscription Plan supports HTTPS Encryption, you may use HTTPS protocol instead of HTTP using `useHTTPS` method:

```
use AmrShawky\Currency;

$accessKey = 'YOUR_API_ACCESS_KEY';

Currency::convert()
        ->setAccessKey($accessKey)
        ->useHTTPS()
        ->from('USD')
        ->to('EUR')
        ->get();
```

### Other Methods

[](#other-methods)

- You may use the `withoutVerifying` method to indicate that TLS certificates should not be verified when sending the request:

```
use AmrShawky\Currency;

$accessKey = 'YOUR_API_ACCESS_KEY';

Currency::convert()
        ->setAccessKey($accessKey)
        ->from('USD')
        ->to('EUR')
        ->withoutVerifying()
        ->get();
```

- You may specify additional [Guzzle request options](https://docs.guzzlephp.org/en/stable/request-options.html "Guzzle request options") using the `withOptions` method. The `withOptions` method accepts an array of key / value pairs:

```
use AmrShawky\Currency;

$accessKey = 'YOUR_API_ACCESS_KEY';

Currency::rates()
        ->setAccessKey($accessKey)
        ->historical('2021-04-30')
        ->withOptions([
            'debug'   => true,
            'timeout' => 3.0
        ])
        ->get();
```

- The `when` method will execute the given callback when the first argument given to the method evaluates to true:

```
use AmrShawky\Currency;

$accessKey = 'YOUR_API_ACCESS_KEY';

Currency::rates()
        ->setAccessKey($accessKey)
        ->latest()
        ->when(true, function ($rates) {
            // will execute
            $rates->symbols(['USD', 'EUR', 'EGP'])
                  ->base('GBP');
        })
        ->when(false, function ($rates) {
            // won't execute
            $rates->symbols(['HKD']);
        })
        ->get();
```

License
-------

[](#license)

The MIT License (MIT). Please see [LICENSE](../master/LICENSE) for more information.

###  Health Score

20

—

LowBetter than 14% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity37

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

941d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/d1d21ed7ca26a337853c97e7b354637d1993105a7e3d9dccd125766c6b18ce9f?d=identicon)[xDemon](/maintainers/xDemon)

---

Top Contributors

[![amrshawky](https://avatars.githubusercontent.com/u/17335226?v=4)](https://github.com/amrshawky "amrshawky (6 commits)")

---

Tags

phpcurrencyexchange ratescurrency-convertercrypto exchange rates

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/xdemonme-currency/health.svg)

```
[![Health](https://phpackages.com/badges/xdemonme-currency/health.svg)](https://phpackages.com/packages/xdemonme-currency)
```

###  Alternatives

[ujjwal/currency-converter

Currency Converter Class with features of caching and identifying currency from country Code

49225.4k](/packages/ujjwal-currency-converter)[cartalyst/converter

A framework agnostic measurement conversion and formatting package featuring multiple types of measurements and currency conversion.

88434.4k7](/packages/cartalyst-converter)[otherguy/php-currency-api

A PHP API Wrapper to offer a unified programming interface for popular Currency Rate APIs.

2526.2k](/packages/otherguy-php-currency-api)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
