kucoin / kucoin-php-sdk
KuCoin API 的 PHP SDK
v1.1.32
2024-07-05 06:25 UTC
Requires
- php: >=5.5.0
- ext-json: *
- guzzlehttp/guzzle: ^6.0|^7.0
- monolog/monolog: ~1.0|~2.0|~3.0
- ratchet/pawl: ^0.4.1
Requires (Dev)
- phpunit/phpunit: ^5.7
- dev-master
- v1.1.32
- v1.1.31
- v1.1.30
- v1.1.29
- v1.1.28
- v1.1.27
- v1.1.26
- v1.1.25
- v1.1.24
- v1.1.23
- v1.1.22
- v1.1.21
- v1.1.20
- v1.1.19
- 1.1.18
- v1.1.17
- v1.1.16
- v1.1.15
- v1.1.14
- v1.1.13
- v1.1.12
- v1.1.11
- v1.1.10
- v1.1.9
- v1.1.8
- v1.1.7
- v1.1.6
- v1.1.5
- v1.1.4
- v1.1.3
- v1.1.2
- v1.1.1
- v1.1.0
- v1.0.12
- v1.0.11
- v1.0.10
- 1.0.9
- 1.0.8
- 1.0.7
- 1.0.6
- 1.0.5
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
This package is auto-updated.
Last update: 2024-09-13 07:59:05 UTC
README
详细的文档 https://docs.kucoin.com,为了接收最新的 API 变更通知,请
关注
此存储库。
要求
安装
通过 Composer 安装包。
composer require "kucoin/kucoin-php-sdk:~1.1.0"
使用
选择环境
// Switch to the sandbox environment KuCoinApi::setBaseUri('https://api.kucoin.com');
调试模式与日志记录
// Debug mode will record the logs of API and WebSocket to files in the directory "KuCoinApi::getLogPath()" according to the minimum log level "KuCoinApi::getLogLevel()". KuCoinApi::setDebugMode(true); // Logging in your code // KuCoinApi::setLogPath('/tmp'); // KuCoinApi::setLogLevel(Monolog\Logger::DEBUG); KuCoinApi::getLogger()->debug("I'm a debug message");
示例
查看更多示例的 测试用例。
未认证的 API 示例
use KuCoin\SDK\PublicApi\Time; $api = new Time(); $timestamp = $api->timestamp(); var_dump($timestamp);
注意
为了加强 API 的安全性,KuCoin 将 API 密钥升级到 2.0 版本,验证逻辑也发生了变化。建议您创建 (https://www.kucoin.com/account/api) 并将 API 密钥更新到 2.0 版本。1.0 版本的 API 密钥将有效至 2021 年 5 月 1 日。
认证的 API 示例
use KuCoin\SDK\PrivateApi\Account; use KuCoin\SDK\Exceptions\HttpException; use KuCoin\SDK\Exceptions\BusinessException; use KuCoin\SDK\Auth; // Auth version v2 (recommend) $auth = new Auth('key', 'secret', 'passphrase', Auth::API_KEY_VERSION_V2); // Auth version v1 // $auth = new Auth('key', 'secret', 'passphrase'); $api = new Account($auth); try { $result = $api->getList(['type' => 'main']); var_dump($result); } catch (HttpException $e) { var_dump($e->getMessage()); } catch (BusinessException $e) { var_dump($e->getMessage()); }
WebSocket 推送示例
use KuCoin\SDK\Auth; use KuCoin\SDK\PrivateApi\WebSocketFeed; use Ratchet\Client\WebSocket; use React\EventLoop\Factory; use React\EventLoop\LoopInterface; $auth = null; // Need to pass the Auth parameter when subscribing to a private channel($api->subscribePrivateChannel()). // $auth = new Auth('key', 'secret', 'passphrase'); $api = new WebSocketFeed($auth); // Use a custom event loop instance if you like //$loop = Factory::create(); //$loop->addPeriodicTimer(1, function () { // var_dump(date('Y-m-d H:i:s')); //}); //$api->setLoop($loop); $query = ['connectId' => uniqid('', true)]; $channels = [ ['topic' => '/market/ticker:KCS-BTC'], // Subscribe multiple channels ['topic' => '/market/ticker:ETH-BTC'], ]; $api->subscribePublicChannels($query, $channels, function (array $message, WebSocket $ws, LoopInterface $loop) use ($api) { var_dump($message); // Subscribe another channel // $ws->send(json_encode($api->createSubscribeMessage('/market/ticker:LTC-BTC'))); // Unsubscribe the channel // $ws->send(json_encode($api->createUnsubscribeMessage('/market/ticker:ETH-BTC'))); // Stop loop // $loop->stop(); }, function ($code, $reason) { echo "OnClose: {$code} {$reason}\n"; });
添加自定义选项
use KuCoin\SDK\PublicApi\Time; $api = new Time(null, new GuzzleHttp([ 'curl' => [ // custom cURL options: https://php.ac.cn/manual/en/function.curl-setopt CURLOPT_TCP_NODELAY => true, // Disable TCP's Nagle algorithm, which tries to minimize the number of small packets on the network. // ... ], ])); $timestamp = $api->timestamp(); var_dump($timestamp);
use KuCoin\SDK\Auth; use KuCoin\SDK\Http\GuzzleHttp; use KuCoin\SDK\KuCoinApi; use KuCoin\SDK\PrivateApi\WebSocketFeed; use Ratchet\Client\WebSocket; use React\EventLoop\Factory; use React\EventLoop\LoopInterface; $api = new WebSocketFeed( null, new GuzzleHttp([ 'curl' => [ // Custom cURL options: https://php.ac.cn/manual/en/function.curl-setopt CURLOPT_TCP_NODELAY => true, // Disable TCP's Nagle algorithm, which tries to minimize the number of small packets on the network. // ... ], ]) ); $query = ['connectId' => uniqid('', true)]; $channels = [ ['topic' => '/market/ticker:KCS-BTC'], ['topic' => '/market/ticker:ETH-BTC'], ]; $options = ['tcp' => ['tcp_nodelay' => true]]; // Custom socket context options: https://php.ac.cn/manual/zh/context.socket $api->subscribePublicChannels($query, $channels, function (array $message, WebSocket $ws, LoopInterface $loop) use ($api) { var_dump($message); }, function ($code, $reason) { echo "OnClose: {$code} {$reason}\n"; }, $options);
⚡️异步 IO 的协程 HTTP 客户端
查看 基准测试,比
curl
快近20x
。
pecl install swoole composer require swlib/saber
use KuCoin\SDK\Auth; use KuCoin\SDK\Http\SwooleHttp; use KuCoin\SDK\KuCoinApi; use KuCoin\SDK\PrivateApi\Order; use KuCoin\SDK\PublicApi\Time; // Require PHP 7.1+ and Swoole 2.1.2+ // Require running in cli mode go(function () { $api = new Time(null, new SwooleHttp); $timestamp = $api->timestamp(); var_dump($timestamp); }); go(function () { // Auth version v2 (recommend) $auth = new Auth('key', 'secret', 'passphrase', Auth::API_KEY_VERSION_V2); // Auth version v1 // $auth = new Auth('key', 'secret', 'passphrase'); $api = new Order($auth, new SwooleHttp); // Create 50 orders CONCURRENTLY in 1 second for ($i = 0; $i < 50; $i++) { go(function () use ($api, $i) { $order = [ 'clientOid' => uniqid(), 'price' => '1', 'size' => '1', 'symbol' => 'BTC-USDT', 'type' => 'limit', 'side' => 'buy', 'remark' => 'ORDER#' . $i, ]; try { $result = $api->create($order); var_dump($result); } catch (\Throwable $e) { var_dump($e->getMessage()); } }); } });
API 列表
KuCoin\SDK\PrivateApi\Account
KuCoin\SDK\PrivateApi\Deposit
KuCoin\SDK\PrivateApi\TradeFee
KuCoin\SDK\PrivateApi\Symbol
KuCoin\SDK\PrivateApi\Order
KuCoin\SDK\PrivateApi\OrderTest
KuCoin\SDK\PrivateApi\OcoOrder
KuCoin\SDK\PrivateApi\Earn
KuCoin\SDK\PrivateApi\StopOrder
KuCoin\SDK\PrivateApi\Fill
KuCoin\SDK\PrivateApi\WebSocketFeed
KuCoin\SDK\PrivateApi\Withdrawal
KuCoin\SDK\PublicApi\Currency
KuCoin\SDK\PublicApi\Symbol
KuCoin\SDK\PrivateApi\Margin
KuCoin\SDK\PrivateApi\Lend
KuCoin\SDK\PrivateApi\IsolatedMargin
KuCoin\SDK\PublicApi\Time
KuCoin\SDK\PublicApi\ServiceStatus
运行测试
首先在
phpunit.xml
中修改您的 API 密钥。
# Add your API configuration items into the environmental variable first export API_BASE_URI=https://api.kucoin.com export API_KEY=key export API_SECRET=secret export API_PASSPHRASE=passphrase export API_KEY_VERSION=2 composer test