承接 xdemonme/currency 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

xdemonme/currency

Composer 安装命令:

composer require xdemonme/currency

包简介

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

README 文档

README

Tests Packagist License Packagist Version Packagist Downloads

This package supports exchangerate.host API keys.

Currency is a simple PHP library for current and historical currency exchange rates & crypto exchange rates. based on the free API exchangerate.host!

Requirements

  • PHP >= 7.2
  • guzzlehttp >= 6.0

Installation

composer require xdemonme/currency

Usage

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

  • 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

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

  • 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

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

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)

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

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

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

  • 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 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

The MIT License (MIT). Please see LICENSE for more information.

xdemonme/currency 适用场景与选型建议

xdemonme/currency 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 129 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 10 月 20 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「php」 「currency」 「currency converter」 「exchange rates」 「crypto exchange rates」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 xdemonme/currency 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 xdemonme/currency 我们能提供哪些服务?
定制开发 / 二次开发

基于 xdemonme/currency 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 129
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 19
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 11
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-10-20