PHPackages                             yzh52521/think-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. [Utility &amp; Helpers](/categories/utility)
4. /
5. yzh52521/think-currency

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

yzh52521/think-currency
=======================

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

v1.0.0(4y ago)5169MITPHPPHP &gt;=7.2

Since May 19Pushed 4y ago1 watchersCompare

[ Source](https://github.com/yzh52521/think-currency)[ Packagist](https://packagist.org/packages/yzh52521/think-currency)[ RSS](/packages/yzh52521-think-currency/feed)WikiDiscussions main Synced 1mo ago

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

Think Currency
==============

[](#think-currency)

[![Tests](https://github.com/amrshawky/laravel-currency/workflows/Tests/badge.svg?branch=master)](https://github.com/amrshawky/laravel-currency/workflows/Tests/badge.svg?branch=master) [![Packagist License](https://camo.githubusercontent.com/a768bfe12d34fe766c8987c311b6a4761badc3afa28a2a9972f54626198267ad/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f616d72736861776b792f6c61726176656c2d63757272656e63793f636f6c6f723d73756363657373266c6162656c3d4c6963656e7365)](https://camo.githubusercontent.com/a768bfe12d34fe766c8987c311b6a4761badc3afa28a2a9972f54626198267ad/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f616d72736861776b792f6c61726176656c2d63757272656e63793f636f6c6f723d73756363657373266c6162656c3d4c6963656e7365) [![Packagist Version](https://camo.githubusercontent.com/9d48f8d79202eeb0bb884dd72cd6289c3d19f2d9cca2e48f78d09b14908acdf3/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f616d72736861776b792f6c61726176656c2d63757272656e63793f6c6162656c3d5061636b6167697374)](https://camo.githubusercontent.com/9d48f8d79202eeb0bb884dd72cd6289c3d19f2d9cca2e48f78d09b14908acdf3/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f616d72736861776b792f6c61726176656c2d63757272656e63793f6c6162656c3d5061636b6167697374) [![Packagist Downloads](https://camo.githubusercontent.com/9ce0dd4d122fd474230ac794dfd815ff9599833498ffd6a6de14690e7454bc43/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f616d72736861776b792f6c61726176656c2d63757272656e63793f636f6c6f723d73756363657373266c6162656c3d446f776e6c6f616473)](https://camo.githubusercontent.com/9ce0dd4d122fd474230ac794dfd815ff9599833498ffd6a6de14690e7454bc43/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f616d72736861776b792f6c61726176656c2d63757272656e63793f636f6c6f723d73756363657373266c6162656c3d446f776e6c6f616473)

Laravel currency is a simple package for current and historical currency exchange rates &amp; crypto exchange rates. based on the free API [exchangerate.host](https://exchangerate.host "exchangerate.host Homepage") - no API keys needed!

> Note: This package is an integration for the [Currency](https://github.com/amrshawky/currency) library

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

[](#requirements)

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

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

[](#installation)

```
composer require yzh52521/think-currency

```

Usage
-----

[](#usage)

### 1. Currency Conversion

[](#1-currency-conversion)

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

```
use yzh52521\ThinkCurrency\facade\Currency;

Currency::convert()
        ->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 yzh52521\ThinkCurrency\facade\Currency;

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

#### Available Methods

[](#available-methods)

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

```
use yzh52521\ThinkCurrency\facade\Currency;

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

- Round the converted amount to decimal places:

```
use yzh52521\ThinkCurrency\facade\Currency;

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

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

```
use yzh52521\ThinkCurrency\facade\Currency;

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

### 2. Latest Rates

[](#2-latest-rates)

To get latest rates you may chain the methods:

```
use yzh52521\ThinkCurrency\facade\Currency;

Currency::rates()
        ->latest()
        ->get();

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

Currency::rates()
        ->latest()
        ->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 yzh52521\ThinkCurrency\facade\Currency;

Currency::rates()
        ->latest()
        ->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 yzh52521\ThinkCurrency\facade\Currency;

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

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

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

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

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

```
use yzh52521\ThinkCurrency\facade\Currency;

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

### 4. Timeseries Rates

[](#4-timeseries-rates)

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 yzh52521\ThinkCurrency\facade\Currency;

Currency::rates()
        ->timeSeries('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. Fluctuations

[](#5-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 yzh52521\ThinkCurrency\facade\Currency;

Currency::rates()
        ->fluctuations('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 yzh52521\ThinkCurrency\facade\Currency;

Currency::convert()
        ->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 yzh52521\ThinkCurrency\facade\Currency;

Currency::convert()
        ->from('USD')
        ->to('EUR')
        ->amount(20)
        ->throw(function ($response, $e) {
            //
        })
        ->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 yzh52521\ThinkCurrency\facade\Currency;

Currency::convert()
        ->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 yzh52521\ThinkCurrency\facade\Currency;

Currency::rates()
        ->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 yzh52521\ThinkCurrency\facade\Currency;

Currency::rates()
        ->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();
```

### Testing

[](#testing)

Currency uses Laravel facades which makes it easy to [mock](https://laravel.com/docs/8.x/mocking#mocking-facades "Mocking Laravel Facades") so it's not actually executed during the test:

```
use yzh52521\ThinkCurrency\facade\Currency;

Currency::shouldReceive('convert')
        ->once()
        ->andReturn(1.50);

Currency::shouldReceive('rates')
         ->once()
         ->andReturn(['EUR' => 1,'USD' => 1.215707]);
```

More information regarding list of bank sources [here](https://api.exchangerate.host/sources "List of bank sources")

For a list of all supported symbols [here](https://api.exchangerate.host/symbols "List of supported symbols") and list of crypto currencies [here](https://api.exchangerate.host/cryptocurrencies)

License
-------

[](#license)

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

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity15

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity46

Maturing project, gaining track record

 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

1820d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/15060466?v=4)[听风吹雨](/maintainers/yuanzhihai)[@yuanzhihai](https://github.com/yuanzhihai)

---

Top Contributors

[![yuanzhihai](https://avatars.githubusercontent.com/u/15060466?v=4)](https://github.com/yuanzhihai "yuanzhihai (7 commits)")

---

Tags

currencyexchange ratescurrency-converterthinkphp

### Embed Badge

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

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

###  Alternatives

[grumpydictator/firefly-iii

Firefly III: a personal finances manager.

22.8k69.3k](/packages/grumpydictator-firefly-iii)[florianv/swap

Exchange rates library for PHP

1.3k6.4M16](/packages/florianv-swap)[ujjwal/currency-converter

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

49225.4k](/packages/ujjwal-currency-converter)[florianv/laravel-swap

Currency exchange rates library for Laravel and Lumen

3342.0M2](/packages/florianv-laravel-swap)[firefly-iii/data-importer

Firefly III Data Import Tool.

7545.8k](/packages/firefly-iii-data-importer)[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)
