oasin / cryptocurrencies-laravel
Laravel 用于加密货币API服务的包
v0.1.2
2019-01-09 20:28 UTC
Requires
- php: ^7.1.3
- guzzlehttp/guzzle: ^6.0
Requires (Dev)
- orchestra/testbench: ^3.7
- phpunit/phpunit: ^7.5
This package is auto-updated.
Last update: 2024-09-11 04:35:50 UTC
README
Laravel 的加密货币API包装器,提供了一种使用第三方API的简便方法。从加密货币管理器中恢复网关,开始查询所选API。
要求
Cryptocurrencies API Laravel Manager 的最低要求是安装 Laravel 5.7,并且您的Web服务器支持 PHP 7.1。
安装
要安装 Cryptocurrencies API Laravel Manager 包,您可以使用简单的composer命令。
composer require ilcleme/cryptocurrencies-laravel
发布供应商
此步骤是使用每个API调用所必需的。安装包后,您可以发布配置包文件,使用以下命令
php artisan vendor:publish --provider="Oasin\\Cryptocurrencies\\Providers\\CryptocurrenciesServiceProvider"
现在您可以覆盖默认配置,并插入您个人 每个第三方服务的API密钥。
架构
该包包含3个主要对象,这些对象已被用于与蜜蜂交互
- Cryptocurrencies API Manager (
cryptocurrencies.manager是 Laravel 的容器别名); - 网关,即用于查询第三方API的对象,每个都有用于从管理器中检索它的名称;
可用的网关
- 对于 Cryptocompare,有4个“开箱即用”网关可用,每个对应于 Cryptocompare 文档中可用的一个部分
- Cryptocompare "一般信息",用于“一般信息”部分的每个API调用;
- Cryptocompare "历史数据",用于“历史数据”部分的每个API调用;
- Cryptocompare "价格数据",用于“价格”部分的每个API调用;
- Cryptocompare "排行榜数据",每个“排行榜”部分的API调用网关;
- 对于 Coinmarketcap,只有一个可用的网关,通过它可以执行任何类型的请求。
用法
要使用此包,只需从容器中获取 Cryptocurrencies API Manager 实例。根据您要进行的查询类型获取选择的网关,例如,要“获取BTC在法定货币中的价格”请使用 Cryptocompare "价格数据"。使用网关方法并传递正确的参数来查询API。例如
namespace App\Http\Controllers; use Oasin\Cryptocurrencies\Gateways\Cryptocompare\CryptocomparePriceGateway; class CryptoController extends Controller { public function getBTCPrice() { $manager = app('cryptocurrencies.manager'); /** @var CryptocomparePriceGateway $priceGateway */ $priceGateway = $manager->getGateway('price'); //parameters passed to the method are defined in accordance with the Cryptocompare documentation of endpoint $parameters = [ 'fsym' => 'BTC', 'tsyms' => 'USD,JPY,EUR' ]; $result = $priceGateway->getSingleSymbolPrice($parameters); return $result; } }
为这个控制器设置路由并期待收到类似的响应
{ "USD": 3887.35, "JPY": 412906.65, "EUR": 3399.46 }
使用 Coinmarketcap 网关的方式相同
namespace App\Http\Controllers; use Oasin\Cryptocurrencies\Gateways\Coinmarketcap\CoinmarketcapGateway; class CryptoController extends Controller { public function test() { $manager = app('cryptocurrencies.manager'); /** @var CoinmarketcapGateway $coinmarketcapGateway */ $coinmarketcapGateway = $manager->getGateway('coinmarketcap'); //parameters passed to the method are defined in accordance with the Coinmarketcap documentation of endpoint $parameters = [ 'query' => ['id' => '1,2'] ]; $result = $coinmarketcapGateway->send('/v1/cryptocurrency/info', 'GET', $parameters); return $result; } }
预期的响应
{
"status": {
"timestamp": "2019-01-03T11:43:39.871Z",
"error_code": 0,
"error_message": null,
"elapsed": 4,
"credit_count": 1
},
"data": {
"1": {
"urls": {
"website": ["https://bitcoin.org/"],
"twitter": [],
"reddit": ["https://reddit.com/r/bitcoin"],
"message_board": ["https://bitcointalk.org"],
"announcement": [],
"chat": [],
"explorer": [
"https://blockchain.info/",
"https://live.blockcypher.com/btc/",
"https://blockchair.com/bitcoin/blocks"
],
"source_code": ["https://github.com/bitcoin/"]
},
"logo": "https://s2.coinmarketcap.com/static/img/coins/64x64/1.png",
"id": 1,
"name": "Bitcoin",
"symbol": "BTC",
"slug": "bitcoin",
"date_added": "2013-04-28T00:00:00.000Z",
"tags": ["mineable"],
"platform": null,
"category": "coin"
},
"2": {
"urls": {
"website": ["https://litecoin.com"],
"twitter": ["https://twitter.com/LitecoinProject"],
"reddit": ["https://reddit.com/r/litecoin"],
"message_board": ["https://litecointalk.io/"],
"announcement": ["https://bitcointalk.org/index.php?topic=47417.0"],
"chat": ["https://telegram.me/litecoin"],
"explorer": [
"http://explorer.litecoin.net/chain/Litecoin",
"https://chainz.cryptoid.info/ltc/",
"https://live.blockcypher.com/ltc/"
],
"source_code": ["https://github.com/litecoin-project/litecoin"]
},
"logo": "https://s2.coinmarketcap.com/static/img/coins/64x64/2.png",
"id": 2,
"name": "Litecoin",
"symbol": "LTC",
"slug": "litecoin",
"date_added": "2013-04-28T00:00:00.000Z",
"tags": ["mineable"],
"platform": null,
"category": "coin"
}
}
}
网关和API参考
这里有 Cryptocompare API 的完整API和网关参考
这里有 Coinmarketcap API 的完整API和网关参考
测试
要运行包测试,您需要安装开发需求(测试工具)并从包文件夹运行 phpunit
composer install --dev
vendor/bin/phpunit tests
如果您想获得更详细的日志,请使用此命令
vendor/bin/phpunit --testdox tests