ineersa/converter

此包的最新版本(v1.0.1)没有可用的许可信息。

货币转换器

v1.0.1 2016-02-13 13:45 UTC

This package is auto-updated.

Last update: 2024-09-24 22:45:37 UTC


README

简单的货币转换API。

目前使用欧洲央行(ECB)货币汇率服务。 http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml

安装

composer require ineersa/converter

功能

  • 结果缓存
  • 易于扩展
  • 快吗?

使用示例

基本用法

use Ineersa\Converter\Converter;
use Ineersa\Converter\Services\EcbService;

$converter = new Converter(new EcbService());
echo $converter->convert('USD','RUB',100);

获取可用的货币列表

$service = new EcbService();
$service->getCurrenciesList();

获取欧元单币种汇率

$service = new EcbService();
echo $service->getRate('USD');

您可以通过服务控制要使用的客户端、适配器或缓存。例如

$cache = new MemcacheCache();
$service = new EcbService($cache);
echo $service->getRate('USD');

openexchangerates在版本1.0.1中添加

要获取appp_id令牌,请访问openexchangerates并注册。

存在免费计划(每月1000次请求),这对于简单使用来说已经足够。

###使用示例

基本用法

    $service = new OpenExchangeRatesService();
    $service->getClient()->setAppId('YOUR_APP_ID_TOKEN');
    $converter = new Converter($service);
    echo $converter->convert('USD','RUB',1);

付费计划的特定功能

    $service = new OpenExchangeRatesService();
    $service->getClient()->setAppId('YOUR_APP_ID_TOKEN');
    $service->getClient()->setBase('EUR');//You can control your base currency
    $service->getClient()->setSymbols('EUR,USD,RUB');//You can control what currencies you need in response
    $converter = new Converter($service);
    echo $converter->convert('USD','RUB',1);

默认使用Memcached、Guzzle。要使用自己的,实现相应的接口。

演示