mrzard / open-exchange-rates-bundle
为 Symfony2 暴露 OpenExchangeRates API
v3.0.1
2017-12-01 14:27 UTC
Requires
- php: >=5.6.0
- guzzlehttp/guzzle: ~6
- mrzard/open-exchange-rates-service: 1.1.1
- symfony/symfony: >=2.8.0
Requires (Dev)
- phpunit/phpunit: >=4.0.0
README
版本系列
分支 2.0.0 将支持 Symfony >= 2.3
分支 3.0.0 将支持 Symfony >= 3 (进行中)
安装
$ php composer.phar require mrzard/open-exchange-rates-bundle ~2.0.0
并在您的 AppKernel.php 文件中注册该包
return array( // ... new Mrzard\OpenExchangeRatesBundle\OpenExchangeRatesBundle(), // ... );
配置
您需要定义您的 API ID 在您环境的 parameters.yml 文件中。
然后,将以下内容添加到您的 services.yml 文件中
open_exchange_rates: api_id: %YOUR_API_ID_PARAM% api_configuration: https: true|false #defaults to false base_currency: XXX #defaults to USD
如果您使用的是免费版本,则无需更改 https 或 base_currency,因为它们仅适用于企业/无限账户
用法
您可以通过在容器中获取 open_exchange_rates_service 来访问此服务
请注意,某些选项仅适用于企业/无限计划
免费功能
获取最新汇率
/** * Get the latest exchange rates * * @param array $symbols Currency codes to get the rates for. Default all * @param string $base Base currency, default NULL (gets it from config) * * @return array */ public function getLatest($symbols = array, $base = null) { }
只有拥有企业或无限计划,才使用 $symbols 和 $base 参数。
输出
array (size=5)
'disclaimer' => string 'Exchange rates...'
'license' => string 'Data sourced from...'
'timestamp' => int 1395396061
'base' => string 'USD' (length=3)
'rates' =>
array (size=166)
'AED' => float 3.672721
'AFN' => float 56.747225
'ALL' => float 101.7573
'AMD' => float 417.366998
...
)
)
获取可用货币
/** * Gets a list of all available currencies * * @return array with keys = ISO codes, content = Currency Name */ public function getCurrencies() { }
输出
array (size=5)
'AED' => 'United Arab Emirates Dirham'
'AFN' => 'Afghan Afghani'
'ALL' => 'Albanian Lek'
'AMD' => 'Armenian Dram'
'ANG' => 'Netherlands Antillean Guilder'
...
)
获取某日期的历史数据
/** * Get historical data * * @param \DateTime $date */ public function getHistorical(\DateTime $date) { }
输出
array (size=5)
'disclaimer' => string 'Exchange rates...'
'license' => string 'Data sourced from...'
'timestamp' => int 1388617200
'base' => string 'USD' (length=3)
'rates' =>
array (size=166)
'AED' => float 3.672524
'AFN' => float 56.0846
'ALL' => float 102.06575
'AMD' => float 408.448002
'ANG' => float 1.78902
'AOA' => float 97.598401
'ARS' => float 6.51658
'AUD' => float 1.124795
'AWG' => float 1.789775
'AZN' => float 0.7841
'BAM' => float 1.421715
'BBD' => int 2
...
)
)
开发者/无限功能
获取最新汇率,限制返回数组
$openExchangeRatesService->getLatest(['EUR', 'USD', 'COP']);
输出
array (size=5)
'disclaimer' => string 'Exchange rates ...'
'license' => string 'Data sourced...'
'timestamp' => int 1395396061
'base' => string 'USD' (length=3)
'rates' =>
array (size=3)
'EUR' => ...,
'USD' => ...,
'COP' => ...
)
)
您还可以通过第二个参数更改获取最新汇率时使用的基准货币
直接在货币之间转换数量
$openExchangeRatesService->convert(10, 'USD', 'EUR');