abdelrahmanbadr / currency-rates
与exchangeratesapi货币汇率API交互的库
dev-master
2020-06-13 22:14 UTC
Requires
- php: ^7.1.3
- guzzlehttp/guzzle: ~6.0
- phpoffice/phpspreadsheet: ^1.6
- vlucas/phpdotenv: ^3.3@dev
Requires (Dev)
- illuminate/support: ^5.4
- mockery/mockery: ^1.2
- phpmd/phpmd: dev-master
- phpunit/phpunit: ^7.0
- squizlabs/php_codesniffer: ^3.0@dev
This package is auto-updated.
Last update: 2024-09-04 14:23:09 UTC
README
安装
Currency Rates Package支持Laravel 5.5及以上版本。
通过composer安装包
- composer require abdelrahmanbadr/currency-rates
- 在.env文件中设置exchangeratesapi URL到EXCHANGE_RATES_API_URL
- 在.env文件中设置缓存启用或禁用(true或false)CURRENCY_RATES_CACHE_IS_ENABLED
- 在.env文件中设置缓存过期时间(整数)CURRENCY_RATES_CACHE_EXPIRY
使用方法
让我们获取以USD为基础货币的EUR和GBP的汇率
use Abdelrahman_badr\CurrencyRates\Core\Facades\CurrencyService; $currency = CurrencyService::getLatest("EUR",["GBP","USD"]); $rates = $currency->rates;
默认情况下,基础货币是EUR
,因此如果您的基准货币是EUR,则无需设置它。符号也可以省略,因为Exchange Rates Api将返回所有支持货币。
没有基础货币和货币的简化示例
$currency = CurrencyService::getLatest();
historical
选项将返回您指定日期以来每一天的货币汇率。基础货币和符号也可以省略,但让我们看一个完整的示例
$currency = CurrencyService::getHistorical(new \DateTime(),(new \DateTime())->modify('-15 days'),"EUR",["GBP","USD"]);
这将获取从15天前到今天的可用汇率
export latest rates to csv
将文件名和与getLatest
函数相同的参数
CurrencyService::exportLatest('fliename');
export historical rates to csv
将文件名和与getHistorical
函数相同的参数
CurrencyService::exportHistorical('fliename',new \DateTime(),new \DateTime("2019-01-01"));
运行单元测试
$ ./vendor/bin/phpunit
希望有(@Todo)
1. Add Cli Command To Get Currency Exchange Rates
2- More validation for package user inputs
3- Create convert service like convert 100 USD to EUR
4- Add more providers such as Fixerio and make config switch between them
5- Add logs to trace errors
6- Use transform layer for the result
7- Use config file