cmbuckley/exchange-rates
Composer 安装命令:
composer require cmbuckley/exchange-rates
包简介
A package for interacting with the exchangerate.host API
README 文档
README
Table of Contents
Overview
Exchange Rates is a simple PHP package used for interacting with the exchangerate.host API. You can use it to get the latest or historical exchange rates and convert monetary values between different currencies and cryptocurrencies.
Installation
You can install the package via Composer:
composer require cmbuckley/exchange-rates
The package has been developed and tested to work with the following minimum requirements:
- PHP 8.2
Usage
Setup
The exchangerate.host service requires an access key, which you can sign up and request for free. You need to add a payment card to get the key, but you won't be charged if you're on the free plan. You are resticted to 100 requests per month on the free plan.
Set the API key as follows:
use StarSquare\ExchangeRates\Classes\ExchangeRate; $exchangeRates = new ExchangeRate(); $exchangeRates->setServiceOptions([ 'access_key' => '123abc', ]);
The API also does not support HTTPS on the free plan. If you are on a paid plan and want to use HTTPS, pass the tls
service option:
$exchangeRates->setServiceOptions([ 'access_key' => '123abc', 'tls' => true, ]);
Methods
Available Currencies
To get the available currencies that are supported by the package, you can use the currencies() method like so:
$exchangeRates->currencies();
Exchange Rate Between Two Currencies
To get the exchange for one currency to another, you can use the exchangeRate() method.
The example below shows how to get the exchange rate from GBP to EUR for today:
$result = $exchangeRates->exchangeRate('GBP', 'EUR'); // $result: '1.10086'
If a valid date is passed as the third parameter, the exchange rate for that day will be returned. If no date is passed, today's exchange rate will be used.
Exchange Rate Between More Than Two Currencies
It is possible to get the exchange rates for multiple currencies in one call. This can be particularly useful if you are needing to get many exchange rates at once and do not want to make multiple API calls.
To do this, you can use exchangeRate() method and pass an array of currency code strings as the second parameter. This will return an array containing the exchange rates as strings:
$result = $exchangeRates->exchangeRate('GBP', ['EUR', 'USD']); // $result: [ // 'GBPEUR' => '1.10086', // 'GBPUSD' => '1.25622' // ];
As above, you can pass a date as the third parameter to get the exchange rates for that day.
Exchange Rates Between Date Range
To get the exchange rates between two currencies and a range of dates, you can use the exchangeRateBetweenDateRange() method.
The example below shows how to get the exchange rates from GBP to EUR for the past 3 days:
$result = $exchangeRates->exchangeRateBetweenDateRange( 'GBP', 'EUR', (new DateTime)->sub(new DateInterval('P3D')), new DateTime ); // $result: [ // '2020-07-07' => [ // 'GBPEUR' => '1.1092623405', // ], // '2020-07-08' => [ // 'GBPEUR' => '1.1120625424', // ], // '2020-07-09' => [ // 'GBPEUR' => '1.1153867604', // ], // ];
As before, you can pass an array of currency codes as the second parameter:
$result = $exchangeRates->exchangeRateBetweenDateRange( 'GBP', ['EUR', 'USD'], (new DateTime)->sub(new DateInterval('P3D')), new DateTime ); // $result: [ // '2020-07-07' => [ // 'GBPEUR' => '1.1092623405', // 'GBPUSD' => '1.2523571825', // ], // '2020-07-08' => [ // 'GBPEUR' => '1.1120625424', // 'GBPUSD' => '1.2550737853', // ], // '2020-07-09' => [ // 'GBPEUR' => '1.1153867604', // 'GBPUSD' => '1.2650716636', // ], // ];
Convert Currencies
Similar to how you can get the exchange rate from one currency to another, you can also convert a monetary value from one currency to another. To do this you can use the convert() method.
When passing in the monetary value (first parameter) that is to be converted, it's important that you pass it in the lowest denomination of that currency. For example, £1 GBP would be passed in as 100 (as £1 = 100 pence).
The example below shows how to convert £1 to EUR at today's exchange rate:
$result = $exchangeRates->convert(100, 'GBP', 'EUR'); // $result: '110.15884906'
If a valid date is passed as the third parameter, the exchange rate for that day will be used. If no date is passed, today's exchange rate will be used.
You can also use the convert() method to convert a monetary value from one currency to multiple currencies. To do this, you can pass an array of currency codes strings as the third parameter:
$result = $exchangeRates->convert( 100, 'GBP', ['EUR', 'USD'] ); // $result: [ // 'GBPEUR' => '110.15884906', // 'GBPUSD' => '125.30569081' // ];
Convert Currencies Between Date Range
Similar to getting the exchange rates between a date range, you can also get convert monetary values from one currency to another using the exchange rates. To do this you can use the convertBetweenDateRange() method:
$result = $exchangeRates->convertBetweenDateRange( 100, 'GBP', 'EUR', (new DateTime)->sub(new DateInterval('P3D')), new DateTime ); // $result: [ // '2020-07-07' => [ // 'GBPEUR' => '110.92623405', // ], // '2020-07-08' => [ // 'GBPEUR' => '111.20625424', // ], // '2020-07-09' => [ // 'GBPEUR' => '111.53867604', // ], // ];
You can also convert to multiple currencies:
$result = $exchangeRates->convertBetweenDateRange( 100, 'GBP', ['EUR', 'USD'], (new DateTime)->sub(new DateInterval('P3D')), new DateTime ); // $result: [ // '2020-07-07' => [ // 'GBPEUR' => '110.92623405', // 'GBPUSD' => '125.23571825', // ], // '2020-07-08' => [ // 'GBPEUR' => '111.20625424', // 'GBPUSD' => '125.50737853', // ], // '2020-07-09' => [ // 'GBPEUR' => '111.53867604', // 'GBPUSD' => '126.50716636', // ], // ];
Testing
To run the tests for the package, you can use the following command:
composer test
Contribution
If you wish to make any changes or improvements to the package, feel free to make a pull request.
To contribute to this library, please use the following guidelines before submitting your pull request:
- Write tests for any new functions that are added. If you are updating existing code, make sure that the existing tests pass and write more if needed.
- Follow PSR-12 coding standards.
- Make all pull requests to the
mainbranch.
Credits
Changelog
Check the CHANGELOG to get more information about the latest changes.
License
The MIT License (MIT). Please see License File for more information.
cmbuckley/exchange-rates 适用场景与选型建议
cmbuckley/exchange-rates 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 03 月 11 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「exchange rate」 「exchangerate」 「exchangerate.host」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 cmbuckley/exchange-rates 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 cmbuckley/exchange-rates 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 cmbuckley/exchange-rates 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A package for validating Google Cloud's JWT provided by webhooks
A Laravel Pulse card to track users & URL's that are being throttled by your Rate Limiters
A/B Testing of content elements
Fixer.io data provider for Peso
Request rate limit
Adds Exchange Web Services Mail Support to Laravel.
统计信息
- 总下载量: 1
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 19
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-03-11
