andromedalab/fixerio
Composer 安装命令:
composer require andromedalab/fixerio
包简介
Wrapper for Fixer.io
README 文档
README
A thin wrapper for Fixer.io, a service for foreign exchange rates and currency conversion. It provides a few methods to easily construct the url, makes the api call and gives back the response.
Installation
- Add the package to your composer.json file and run
composer update:
{
"require": {
"andromedalab/fixerio": "~1.0"
}
}
Laravel users can use the Facade for even easier access.
- Add
Fadion\Fixerio\ExchangeServiceProvider::classto yourconfig/app.phpfile, inside theprovidersarray. - Add a new alias:
'Exchange' => Fadion\Fixerio\Facades\Exchange::classto yourconfig/app.phpfile, inside thealiasesarray.
Usage
Let's get the rates of EUR and GBP with USD as the base currency:
use Fadion\Fixerio\Exchange; use Fadion\Fixerio\Currency; $exchange = new Exchange(); $exchange->key("YOUR_ACCESS_KEY"); $exchange->base(Currency::USD); $exchange->symbols(Currency::EUR, Currency::GBP); $rates = $exchange->get();
By default, the base currency is EUR, so if that's your base, there's no need to set it. The symbols can be omitted too, as Fixer will return all the supported currencies.
A simplified example without the base and currency:
$rates = (new Exchange())->key("YOUR_ACCESS_KEY")->get();
The historical option will return currency rates for every day since the date you've specified. The base currency and symbols can be omitted here to, but let's see a full example:
$exchange = new Exchange(); $exchange->key("YOUR_ACCESS_KEY"); $exchange->historical('2012-12-12'); $exchange->base(Currency::AUD); $exchange->symbols(Currency::USD, Currency::EUR, Currency::GBP); $rates = $exchange->get();
The timeseries option will return currency rates for every day in date range. Period shouldn't be greater than 1 year. The base currency and symbols can be omitted here to, example:
$exchange = new Exchange(); $exchange->key("YOUR_ACCESS_KEY"); $exchange->timeseries('2012-01-01', '2012-12-31'); $exchange->base(Currency::AUD); $exchange->symbols(Currency::USD, Currency::EUR, Currency::GBP); $rates = $exchange->get();
Finally, you may have noticed the use of the Currency class with currencies as constants. It's just a convenience to prevent errors from typos, but they're completely optional.
This:
$exchange->base(Currency::AUD); $exchange->symbols(Currency::USD, Currency::EUR, Currency::GBP);
is equivalent to:
$exchange->base('AUD'); $exchange->symbols('USD', 'EUR', 'GBP');
Use whatever method fills your needs.
Response
The response is a simple array with currencies as keys and ratios as values. For a request like the following:
$rates = (new Exchange())->key("YOUR_ACCESS_KEY")->symbols(Currency::USD, Currency::GBP)->get();
the response will be an array:
array('GBP' => 0.7009, 'USD' => 1.0666)
which you can access with the keys as strings or using the currency constants:
print $rates['EUR']; print $rates[Currency::GBP];
There is an option to handle the response as an object:
$rates = (new Exchange())->key("YOUR_ACCESS_KEY")->symbols(Currency::USD, Currency::GBP)->getAsObject(); print $rates->USD; print $rates->GBP;
The last option is to return the response as a Result class. This allows access to the full set of properties returned from the feed.
$result = (new Exchange())->key("YOUR_ACCESS_KEY")->symbols(Currency::USD, Currency::GBP)->getResult(); $date = $result->getDate(); // The date the data is from $rates = $result->getRates(); // Array of rates as above $usd = $result->getRate(Currency::USD); // Will return null if there was no value
Error Handling
To handle errors, the package provides 2 exceptions. ConnectionException when http requests go wrong and ResponseException when the returned response from the api is not as expected. An example with exception handling:
use Fadion\Fixerio\Exchange; use Fadion\Fixerio\Exceptions\ConnectionException; use Fadion\Fixerio\Exceptions\ResponseException; try { $exchange = new Exchange(); $exchange->key("YOUR_ACCESS_KEY"); $rates = $exchange->get(); } catch (ConnectionException $e) { // handle } catch (ResponseException $e) { // handle }
Laravel Usage
Nothing changes for Laravel apart from the Facade. It's just a convenience for a tad shorter way of using the package:
use Exchange; use Fadion\Fixerio\Currency; $rates = Exchange::base(Currency::USD)->get();
To use this Facade, you should set your access key in your config/services.php file:
'fixer'=>[ 'key'=>env("FIXER_ACCESS_KEY"), ]
andromedalab/fixerio 适用场景与选型建议
andromedalab/fixerio 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 57.44k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 09 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「fixer」 「exchange」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 andromedalab/fixerio 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 andromedalab/fixerio 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 andromedalab/fixerio 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Custom configuration for PHP CS Fixer used by Serendipity HQ.
Fixing php code style
PHP CS Fixer reporter for Bitbucket Pipeline Reports
A package for validating Google Cloud's JWT provided by webhooks
Automatically detect and fix common Livewire issues, with helpful commands and developer tools to streamline your Laravel development.
Fixer.io data provider for Peso
统计信息
- 总下载量: 57.44k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 22
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-09-02