orkhanahmadov/laravel-currencylayer
Composer 安装命令:
composer require orkhanahmadov/laravel-currencylayer
包简介
Laravel package for currencylayer.com
README 文档
README
Simple Laravel package for integrating with currencylayer.com currency rates.
Requirements
- PHP 7.1 or higher
- Laravel 5.8 or higher
Installation
You can install the package via composer:
composer require orkhanahmadov/laravel-currencylayer
Publish package migration and config files:
php artisan vendor:publish --provider="Orkhanahmadov\LaravelCurrencylayer\LaravelCurrencylayerServiceProvider"
Set your currencylayer.com access key in .env file:
CURRENCYLAYER_ACCESS_KEY=your-key-here
You can find your access key in Currencylayer Dashboard.
Configuration
After publishing configuration file it will be available in config directory as currencylayer.php
It has following settings:
access_key- currencylayer.com access key, by default uses value from.envfilehttps_connection- if set totrueall calls to currencylayer API endpoint will be over HTTPS, instead of default HTTP
Usage
You can type-hint Orkhanahmadov\LaravelCurrencylayer\Contracts\CurrencyService to inject it from container:
use Orkhanahmadov\LaravelCurrencylayer\Contracts\CurrencyService; class CurrencyController { public function index(CurrencyService $currencyService) { $currencyService->live('USD', 'EUR'); } }
Anywhere outside container you can create instance of the service with app() helper:
$currencyService = app('currencylayer'); $currencyService->live('USD', 'EUR');
You can also use provided facade:
\Currencylayer::live('USD', 'EUR');
Available methods
All methods save rates to database table when fetched.
live()
Method fetches live rates from currencylayer.com
$currencyService->live('USD', 'EUR');
First argument is source currency, second argument is converted currency.
You can also pass instance of Orkhanahmadov\LaravelCurrencylayer\Models\Currency as a source currency.
use Orkhanahmadov\LaravelCurrencylayer\Models\Currency; $usd = Currency::where('code', 'USD')->first(); $currencyService->live($usd, 'EUR');
To get live rates for multiple currencies, pass multiple currency codes:
$currencyService->live('USD', 'EUR', 'CHF', 'BTC', 'RUB'); // or $currencyService->live('USD', ['EUR', 'CHF', 'BTC', 'RUB']);
rate()
Method fetches rates for given date from currencylayer.com
$currencyService->rate('USD', '2019-01-25', 'EUR');
First argument is source currency, second argument is date, third argument is converted currency.
You can also pass instance of Orkhanahmadov\LaravelCurrencylayer\Models\Currency as a source currency argument
and instance of Carbon\Carbon as a date argument.
use Carbon\Carbon; use Orkhanahmadov\LaravelCurrencylayer\Models\Currency; $usd = Currency::where('code', 'USD')->first(); $today = Carbon::today(); $currencyService->rate($usd, $today, 'EUR');
To get rates for multiple currencies, pass multiple currency codes:
$currencyService->rate('USD', '2019-01-25', 'EUR', 'CHF', 'BTC', 'RUB'); // or $currencyService->rate('USD', '2019-01-25', ['EUR', 'CHF', 'BTC', 'RUB']);
Commands
Package comes with 2 commands:
php artisan currencylayer:live - fetches live rates and outputs the values. First argument is source currency,
second and next arguments are target currencies:
php artisan currencylayer:live USD EUR CHF
This will fetch live rates for USD to EUR and USD to CHF.
php artisan currencylayer:rate - fetches rates for given date and outputs the values. First argument is source currency,
second argument is date, third and next arguments are target currencies:
php artisan currencylayer:rate USD 2019-01-25 EUR CHF
This will fetch rates for 2019-01-25 for USD to EUR and USD to CHF.
Models
Package comes with 2 database models:
Orkhanahmadov\LaravelCurrencylayer\Models\Currency- stores each fetched currency codesOrkhanahmadov\LaravelCurrencylayer\Models\Rate- stores rate for each currency exchange based on timestamp
Currency model has rateFor() method you can use to get currency rate.
First argument is target currency code or can be instance of Currency.
use Orkhanahmadov\LaravelCurrencylayer\Models\Currency; $usd = Currency::where('code', 'USD')->first(); $rate = $usd->rateFor('EUR'); // or $eur = Currency::where('code', 'EUR')->first(); $rate = $usd->rateFor($eur);
This will fetch latest USD to EUR rate.
You can also pass date or Carbon\Carbon instance as a second argument to get rates for that date:
use Carbon\Carbon; use Orkhanahmadov\LaravelCurrencylayer\Models\Currency; $usd = Currency::where('code', 'USD')->first(); $rate = $usd->rateFor('EUR', '2019-01-25'); // or $today = Carbon::today(); $rate = $usd->rateFor('EUR', $today);
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/laravel-currencylayer 适用场景与选型建议
orkhanahmadov/laravel-currencylayer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 264 次下载、GitHub Stars 达 2, 最近一次更新时间为 2019 年 09 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「currency」 「laravel」 「currencylayer」 「orkhanahmadov」 「laravel-currencylayer」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 orkhanahmadov/laravel-currencylayer 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 orkhanahmadov/laravel-currencylayer 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 orkhanahmadov/laravel-currencylayer 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Currencylayer API PHP client
Fixer.io data provider for Peso
TCMB Currenct Converter
API integration with https://currencylayer.com/
Immutable PHP currency converter that's data-agnostic.
PHP client for currencylayer.com
统计信息
- 总下载量: 264
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 9
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-09-12