dandelionmood / openexchangerates
OpenExchangeRates API 的包装器
Requires
- php: >=5.3.0
- kriswallsmith/buzz: v0.9
Requires (Dev)
- apigen/apigen: 2.8.0
- phpunit/phpunit: 3.7.18
This package is auto-updated.
Last update: 2024-08-28 10:04:37 UTC
README
为 OpenExchangeRates API 提供包装器。
此 JSON API 允许您获取各种货币的汇率,该项目将为您提供简单的类,以便无缝(希望如此)地与之交互。
免责声明:此工具未得到 OpenExchangeRates 的认可,这是一个完全独立的作品。
获取 APP ID
首先,您需要一个 app id 来使它工作,您可以在以下位置获取一个: https://openexchangerates.org/signup
如果您想测试它,您可以在以下位置注册免费计划: https://openexchangerates.org/signup/free
请注意,免费计划将不允许您在 HTTPS 下工作,等等。请参阅底部了解如何使用纯 HTTP API。
安装
您可以通过 Composer 和 Packagist 轻松安装它,有关更多说明请参阅此处
- https://getcomposer.org.cn/doc/00-intro.md
- https://packagist.org.cn/packages/dandelionmood/openexchangerates
获取支持货币
您可以在以下位置查看这些数据: http://openexchangerates.org/api/currencies.json
// You'll need to get an app id. define('OPENEXCHANGERATE_APP_ID', '123-123-123'); $oer = new OpenExchangeRates( OPENEXCHANGERATE_APP_ID ); // You'll get an object with all supported currencies $currencies = $oer->currencies();
获取最新汇率
请参阅此处了解您将获得什么: https://openexchangerates.org/documentation#preview-api-response
$oer = new OpenExchangeRates( OPENEXCHANGERATE_APP_ID ); $latest_rates = $eor->latest(); // With a paying plan, you can change base currency like this : $latest_rates_in_euros = $eor->latest(array('base'=>'EUR'));
获取历史汇率(仅限付费客户)
请参阅此处了解您将获得什么: https://openexchangerates.org/documentation#historical-data
$one_month_ago = strftime('%Y-%m-%d', strtotime('- 1 month')); $rates_last_month = $eor->historical($one_month_ago); // You can list only certain currencies using additionnal parameters $rates_last_month = $eor->historical( $one_month_ago, array('currencies'=>array('EUR','USD')) );
在 HTTP 中工作(适用于免费计划)
您可以工作在 HTTP 中,尽管这不是默认行为(主要是因为您应该优先考虑 HTTPS,以确保没有“中间人”等)。
$oer = new OpenExchangeRates( OPENEXCHANGERATE_APP_ID, OpenExchangeRates::PROTOCOL_HTTP );
更改 HTTP 客户端
默认设置是使用 file_get_contents
与 API 交互。由于此方法在所有地方都不可用,您可以选择使用 curl
,以下是方法
$oer = new OpenExchangeRates( OPENEXCHANGERATE_APP_ID, OpenExchangeRates::PROTOCOL_HTTP, OpenExchangeRates::HTTP_CLIENT_CURL );