amelia / money
一个用于在 PHP 中操作 Open Exchange Rates API 的库
v1.0.0-beta.2
2015-08-28 11:21 UTC
Requires
- php: >= 5.4
- ext-openssl: *
- guzzlehttp/guzzle: 6.*
- nesbot/carbon: 1.*
- psr/http-message: ~1.0
Requires (Dev)
- illuminate/support: ~5.0|~5.1
- phpunit/phpunit: ~4.7
This package is auto-updated.
Last update: 2024-09-08 14:05:55 UTC
README
一个用于操作货币 API 和转换的 PHP 5.4+ 库
当前数据提供者包括
- Open Exchange Rates (https://openexchangerates.org)
计划中
- Her Majesty's Revenue & Customs (http://www.hmrc.gov.uk/softwaredevelopers/2015-exrates.htm)
- XE.com (https://xe.com)
欢迎添加更多!
使用方法
要深入了解 OpenExchangeRates API 实现,请 注册一个应用 ID。这是免费的!
Laravel 5
Money 包含一个 Laravel 5 服务提供者。
只需将 Amelia\Money\MoneyServiceProvider::class
添加到您的 config/app.php
文件中的 providers 数组。
您可以在控制器(或更重要的是,表单请求)中类型提示 Amelia\Money\FactoryInterface
,或使用 app(FactoryInterface::class)
从 IoC 容器中获取它。
<?php use Amelia\Money\FactoryInterface; class FooController extends Controller { public function __construct(FactoryInterface $money) { $money->convert($amount = 130.01, $from = "USD", $to = "GBP"); $money->getBase(); $money->getRates(); $newMoney = $money->base("GBP"); // switch the base currency } }
将以下配置添加到 config/services.php
数组
"money" => [ "api" => env("MONEY_API_TYPE", "openexchangerates"), "key" => env("MONEY_API_KEY", null), ],
无框架使用方法
<?php use Amelia\Money\OpenExchangeRatesFactory; $converter = OpenExchangeRatesFactory::create(["key" => "YOUR_APP_ID"]); $converter->convert(140, "gbp", "nok"); // currency codes are case-insensitive. var_dump($converter->getRates(), $converter->getBase());
转换实例是不可变的。要更改基础货币,请使用 ->base(string $base)
方法。要更改汇率,请创建一个新的对象。