定制 andromedalab/fixerio 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

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::class to your config/app.php file, inside the providers array.
  • Add a new alias: 'Exchange' => Fadion\Fixerio\Facades\Exchange::class to your config/app.php file, inside the aliases array.

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 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 57.44k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 22
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-09-02