orkhanahmadov/currencylayer
Composer 安装命令:
composer require orkhanahmadov/currencylayer
包简介
PHP client for currencylayer.com
README 文档
README
Simple PHP client for currencylayer.com currency rates.
Installation
You can install the package via composer:
composer require orkhanahmadov/currencylayer
Usage
Instantiate Orkhanahmadov\Currencylayer\CurrencylayerClient class and pass your "access key"
use Orkhanahmadov\Currencylayer\CurrencylayerClient; $client = new CurrencylayerClient('your-access-key-here');
You can find your access key in Currencylayer Dashboard.
If you are using paid plan, you can use HTTPS connection to currencylayer.com API.
While instantiating pass true as second parameter to use HTTPS connection.
$client = new CurrencylayerClient('your-access-key-here', true);
Available methods
quotes()
Use this method to fetch live and historical currency rates.
Pass source currency to source() method and rate currency currency() method.
Following example will fetch live rates from USD to EUR.
$client->source('USD')->currency('EUR')->quotes();
You can also pass multiple rate currencies to currency() method:
$client->source('USD')->currency('EUR', 'AUD')->quotes(); // you can also pass currencies as an array $client->source('USD')->currency(['EUR', 'AUD'])->quotes();
If you want fetch rates for specific date, you can pass the date to date() method.
date() method accepts dates as string or instance of DateTimeInterface.
$client->source('USD')->currency('EUR')->date('2019-05-20')->quotes();
quotes() method returns instance of Orkhanahmadov\Currencylayer\Data\Quotes.
This class has following methods that you can use:
source()- Returns source currency (for example,USD)timestamp()- Returns timestamp value from currencylayer API (for example,1432400348)quotes()- Returns array of quotes from currencylayer APIdate()- ReturnsDateTimeInterfacedate. If you fetched live rates this method will returnnull
You can also get rates for each fetched currency using currency name as property:
$quotes = $client->source('USD')->currency(['EUR', 'AUD'])->date('2019-05-20')->quotes(); $qoutes->EUR; // returns USD to EUR rate for 2019-05-20 $qoutes->AUD; // returns USD to AUD rate for 2019-05-20
convert()
Use this method to convert amount in one currency to another currency.
Pass source currency to source() method, rate currency currency() method and amount to convert() method.
Following example will convert 10 USD to GBP using live rates.
$client->source('USD')->currency('GBP')->convert(10);
If you want conversion based on different date's rates, you can pass the date to date() method.
date() method accepts dates as string or instance of DateTimeInterface.
$client->source('USD')->currency('GBP')->date('2019-05-20')->convert(10);
convert() method returns instance of Orkhanahmadov\Currencylayer\Data\Conversion.
This class has following methods that you can use:
fromCurrency()- Returns source currency (for example,USD)toCurrency()- Returns target currency (for example,GBP)timestamp()- Returns timestamp value from currencylayer API (for example,1432400348)amount()- Returns amount that passed toconvert()method (for example,10)quote()- Returns quote between source and target currencies (for example,0.658443)result()- Returns conversion result (for example6.58443)date()- ReturnsDateTimeInterfacedate. If you fetched live rates this method will returnnull
timeframe()
Use this method to show rates between given dates.
Pass source currency to source() method, rate currencies to currency() method and
start date as first argument and end date as second argument to timeframe() method.
Start and end dates can be string of dates or instances of DateTimeInterface.
Following example will return timeframe rates from USD to GBP and EUR between 2010-03-01 and 2010-04-01.
$client->source('USD')->currency('GBP', 'EUR')->timeframe('2010-03-01', '2010-04-01');
timeframe() method returns instance of Orkhanahmadov\Currencylayer\Data\Timeframe.
This class has following methods that you can use:
source()- Returns source currency (for example,USD)startDate()- ReturnsDateTimeInterfacestart dateendDate()- ReturnsDateTimeInterfacestart dateallQuotes()- Returns array quotes grouped by each day between start and end datequotes()- Accepts string date or instance ofDateTimeInterfaceand returns rates for that day
You can also use currency code as function call and pass date to get rates:
$timeframe = $client->source('USD')->currency('GBP', 'EUR')->timeframe('2010-03-01', '2010-04-01'); $timeframe->GBP('2010-03-15'); // returns USD to GBP rate for 2010-03-15 $timeframe->EUR('2010-03-20'); // returns USD to EUR rate for 2010-03-20
change()
Use this method to show currency rate change between given dates.
Pass source currency to source() method, rate currencies to currency() method and
start date as first argument and end date as second argument to change() method.
Start and end dates can be string of dates or instances of DateTimeInterface.
Following example will return rate change from USD to GBP and EUR between 2010-03-01 and 2010-04-01.
$client->source('USD')->currency('GBP', 'EUR')->change('2010-03-01', '2010-04-01');
change() method returns instance of Orkhanahmadov\Currencylayer\Data\Change.
This class has following methods that you can use:
source()- Returns source currency (for example,USD)startDate()- ReturnsDateTimeInterfacestart dateendDate()- ReturnsDateTimeInterfacestart datequotes()- Returns array of currency change rates between start and end daterate()- Accepts currency code as an argument and returns currency rate for given start dateendRate()- Accepts currency code as an argument and returns currency rate for given end dateamount()- Accepts currency code as an argument and currency rate's change in amountpercentage()- Accepts currency code as an argument and currency rate's change in percentage
list()
Use this method to get list of all available currencies.
$client->list();
Method will return array of currencies in currencyCode => currencyName structure.
Testing
composer test
Changelog
Please see CHANGELOG for more information what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email ahmadov90@gmail.com instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
orkhanahmadov/currencylayer 适用场景与选型建议
orkhanahmadov/currencylayer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 413 次下载、GitHub Stars 达 0, 最近一次更新时间为 2019 年 09 月 20 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「currency」 「currencylayer」 「orkhanahmadov」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 orkhanahmadov/currencylayer 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 orkhanahmadov/currencylayer 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 orkhanahmadov/currencylayer 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Laravel middleware for whitelisting/blacklisting client IP addresses
Laravel middleware for setting application locale based on HTTP Accept-Language header
Currencylayer API PHP client
Laravel ZIP file content validator
Laravel model settings for Eloquent
Fixer.io data provider for Peso
统计信息
- 总下载量: 413
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 8
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-09-20