mrzard / open-exchange-rates-service
OpenExchangeRates API 接口
v1.1.2
2017-12-01 14:51 UTC
README
此服务将帮助您在项目中使用 OpenExchangeRates
安装
使用 composer 需求和安装服务
$ php composer.phar require mrzard/open-exchange-rates-service ~1.0.0
配置
创建服务实例时,您需要提供访问 OpenExchangeRates API 所需的基本数据和与 HttpClientInterface
兼容的 HTTP 客户端,例如 GuzzleHttp\Client
use Mrzard\OpenExchangeRates\Service\OpenExchangeRatesService; use GuzzleHttp\Client; ... $apiOptions = array( 'https' => false, 'base_currency' => 'USD' ); new OpenExchangeRatesService( $openExchangeRatesApiId, // your id from openExchangeRatesApi $apiOptions, new Client() ); ...
如果您使用的是免费版本,则无需更改 https
或 base_currency
,因为它们仅适用于企业/无限账户
关于 HTTP 客户端的说明
兼容 在当前上下文中意味着客户端必须具有以下签名的方法
public HttpRequestInterface request(string method, string url = null, array $options = []);
public HttpResponseInterface send(HttpRequestInterface request);
此外,包装器不需要实现该接口的返回值,但必须也是兼容的
用法
免费功能
获取最新的汇率
/** * 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 * @param array $symbols array of currency codes to get the rates for. * Default empty (all currencies) * @param string $base Base currency, default NULL (gets it from config) * */ public function getHistorical(\DateTime $date) { }
仅当您拥有开发者、企业或无限计划时,才使用 $symbols
和 $base
参数。
输出
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(array('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');