samrap/gemini
Composer 安装命令:
composer require samrap/gemini
包简介
A Gemini API client for PHP developers.
README 文档
README
Note: This is currently in early development and not yet ready for production purposes.
This is a PHP client implementation for the Gemini digital asset exchange REST API. It is built on expressive contracts that mirror the available public and private API methods, with a decoupled HTTP implementation for the underlying requests. If you are looking to interact with the Gemini API without worrying about the underlying HTTP requests and responses, then this package is for you.
Installation
In order to talk to the API, you will need to install a compatible HTTP client or adapter. This package utilizes HTTPlug which defines how HTTP messages should be sent and received. You can use any library to send HTTP messages that implements php-http/client-implementation.
Here is a list of all officially supported clients and adapters by HTTPlug: http://docs.php-http.org/en/latest/clients.html
Read more about HTTPlug in their docs.
Note: The Guzzle6 Adapter will be used in the following examples.
Once you have chosen your HTTP implementation, go ahead and install it with this package:
composer require php-http/guzzle6-adapter samrap/gemini
That's all you have to do to get started. The client will automatically find your chosen HTTP implementation under the hood, so you can focus on the fun stuff!
Usage
Basic Usage
In order to talk to the API, you will need to create an instance of the Samrap\Gemini\Gemini class. This class implements the API contracts necessary for you to do everything the REST API has to offer.
The class takes two arguments, a key and secret, which are your API key and secret, respectively. Of course, if you only plan on using the Public APIs, you may ignore these arguments:
use Samrap\Gemini\Gemini; $key = 'mykey'; $secret = '1234abcd' $gemini = new Gemini($key, $secret);
The Gemini client's methods map directly to the available APIs in the documentation. For example, if you want to get data from the Symbols API, simply call the symbols method on the Gemini client:
$gemini = new Gemini(); $symbols = $gemini->symbols(); print_r($symbols); // ["btc-usd", "ethbtc", "ethusd"]
The return value of all API calls is the HTTP response's decoded JSON as an associative array. The Gemini client handles all the HTTP requests and responses, allowing you to focus on the data that matters most.
APIs with more than one word, such as Current Order Book, are called as camelCase versions of themselves. Parameters in the URI scheme are passed as individual arguments, while URL (GET) parameters are given as an associative array. Let's take a look at what it would look like to access the public Current Order Book API:
$gemini = new Gemini(); $symbol = 'ethusd'; $orderBook = $gemini->currentOrderBook($symbol, [ 'limit_bids' => 10, 'limit_asks' => 10, ]);
As with all methods, the return value will be the decoded JSON from the response body. Optional URL parameters can be ignored. Since the limit_bids and limit_asks parameters are optional, we could just as easily write the following:
$gemini = new Gemini(); $symbol = 'ethusd'; $orderBook = $gemini->currentOrderBook($symbol);
Piece of cake!
Private APIs
The Gemini client automatically handles authentication and request signing for you. This means that accessing the Private APIs (APIs that require a session) is just as easy as talking to the public ones. Let's place a new buy order:
$gemini = new Gemini('mykey', '1234abcd'); $order = $gemini->newOrder([ 'client_order_id' => '20150102-4738721', 'symbol' => 'btcusd', 'amount' => '34.12', 'price' => '622.13', 'side' => 'buy', 'type' => 'exchange limit', 'options' => ['maker-or-cancel'], ]);
That's it! If the request was successful, the value of $order will be an associative array of the response JSON. If an error occurred, the Gemini Client throws an exception representing the exact error. See Error Handling for more information.
API Reference
The Gemini client implements two contracts, Samrap\Gemini\PublicApi and Samrap\Gemini\PrivateApi. These contracts contain the API methods and their parameters, should you need a reference.
Error Handling
The Gemini client automatically converts all API errors into exceptions named after the reason in each Error Payload. An AuctionNotOpen error will throw a Samrap\Gemini\Exceptions\AuctionNotOpenException, a ClientOrderIdTooLong will throw a Samrap\Gemini\Exceptions\ClientOrderIdTooLongException, etc. Every exception extends the Samrap\Gemini\Exceptions\GeminiException. This gives you great flexibility to handle specific errors you might expect, while adding a catch-all at the end.
Imagine we are writing a method to allow users to check the status of their orders. A lot can go wrong. A user might enter the incorrect order ID, so we will need to account for that. Additionally, the API might be down for maintenance and we certainly would want to log that information. Just to be safe, we should log any other error that could occur. Ok, let's write it:
use Samrap\Gemini\Exceptions\GeminiException; use Samrap\Gemini\Exceptions\MaintenanceException; use Samrap\Gemini\Exceptions\OrderNotFoundException; // ... public function getOrderStatus($order_id) { try { $status = $this->gemini->orderStatus([ 'order_id' => $order_id, ]) } catch (OrderNotFoundException $e) { return 'The order you searched for does not exist.'; } catch (MaintenanceException $e) { $this->logger->critical($e->getMessage()); return 'The API is currently unavailable'; } catch (GeminiException $e) { $this->logger->warn($e->getMessage()) } return $status['original_amount']; }
As you can see, this is much more expressive than dealing with response status codes or checking the error payload to figure out what to do next. The Gemini client handles all of this for you, allowing you to focus on the requirements of your application.
More
More documentation is coming as development continues.
samrap/gemini 适用场景与选型建议
samrap/gemini 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 537 次下载、GitHub Stars 达 5, 最近一次更新时间为 2018 年 02 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「api」 「client」 「bitcoin」 「cryptocurrency」 「ethereum」 「Gemini」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 samrap/gemini 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 samrap/gemini 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 samrap/gemini 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
The BlockTrail PHP SDK, for integration of Bitcoin functionality through the BlockTrail API
A PSR-7 compatible library for making CRUD API endpoints
Mock PSR-18 HTTP client
Asynchronous MQTT client built on React
CoinDesk Bitcoin Price Index API for Laravel
Client for Google Directions API to add interpolated points to a route consisting of given coordinates.
统计信息
- 总下载量: 537
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 25
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-02-23