kucoin/kucoin-futures-php-sdk

KuCoin期货API的PHP SDK

v1.0.24 2024-05-29 02:22 UTC

README

详细的文档https://docs.kucoin.com/futures/,为了接收最新的API变更通知,请关注此仓库。

Latest Version PHP Version Build Status Total Downloads License

需求

安装

通过Composer安装包。

composer require "kucoin/kucoin-futures-php-sdk:~1.0.0"

用法

选择环境

use KuCoin\Futures\SDK\KuCoinFuturesApi;
// Switch to the sandbox environment
KuCoinFuturesApi::setBaseUri('https://api-sandbox-futures.kucoin.com');

调试模式与日志记录

use KuCoin\Futures\SDK\KuCoinFuturesApi;
// Debug mode will record the logs of API and WebSocket to files in the directory "KuCoinFuturesApi::getLogPath()" according to the minimum log level "KuCoinFuturesApi::getLogLevel()".
KuCoinFuturesApi::setDebugMode(true);

// Logging in your code
// KuCoinFuturesApi::setLogPath('/tmp');
// KuCoinFuturesApi::setLogLevel(Monolog\Logger::DEBUG);
KuCoinFuturesApi::getLogger()->debug("I'm a debug message");

示例

查看更多示例的测试用例

未认证的API示例

use KuCoin\Futures\SDK\PublicApi\Time;

$api = new Time();
$timestamp = $api->timestamp();
var_dump($timestamp);

认证的API示例

注意

为了加强API的安全性,KuCoin将API密钥升级到2.0版本,验证逻辑也发生了变化。建议您创建(https://www.kucoin.com/account/api)并更新您的API密钥到2.0版本。1.0版本的API密钥将仍然有效,直到2021年5月1日

use KuCoin\Futures\SDK\Auth;
use KuCoin\Futures\SDK\PrivateApi\Account;
use KuCoin\Futures\SDK\Exceptions\HttpException;
use KuCoin\Futures\SDK\Exceptions\BusinessException;

// 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->getOverview();
    var_dump($result);
} catch (HttpException $e) {
    var_dump($e->getMessage());
} catch (BusinessException $e) {
    var_dump($e->getMessage());
}

WebSocket数据流示例

use KuCoin\Futures\SDK\Auth;
use KuCoin\Futures\SDK\KuCoinFuturesApi;
use KuCoin\Futures\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 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 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('/contractMarket/ticker:ETHUSDTM')));

    // Unsubscribe the channel
    // $ws->send(json_encode($api->createUnsubscribeMessage('/contractMarket/ticker:XBTUSDM')));

    // Stop loop
    // $loop->stop();
}, function ($code, $reason) {
    echo "OnClose: {$code} {$reason}\n";
});

⚡️异步IO的协程HTTP客户端

查看基准测试,几乎比curl20倍

pecl install swoole
composer require swlib/saber
use KuCoin\Futures\SDK\Auth;
use KuCoin\Futures\SDK\Http\SwooleHttp;
use KuCoin\Futures\SDK\KuCoinFuturesApi;
use KuCoin\Futures\SDK\PrivateApi\Order;
use KuCoin\Futures\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\Futures\SDK\PrivateApi\Account
KuCoin\Futures\SDK\PrivateApi\Deposit
KuCoin\Futures\SDK\PrivateApi\Fill
KuCoin\Futures\SDK\PrivateApi\Order
KuCoin\Futures\SDK\PrivateApi\Position
KuCoin\Futures\SDK\PrivateApi\WebSocketFeed
KuCoin\Futures\SDK\PrivateApi\Withdrawal
KuCoin\Futures\SDK\PrivateApi\RiskLimitLevel
KuCoin\Futures\SDK\PublicApi\Symbol
KuCoin\Futures\SDK\PublicApi\Time
KuCoin\Futures\SDK\PublicApi\Status

运行测试

首先修改phpunit.xml中的API密钥。

# Add your API configuration items into the environmental variable first
export API_BASE_URI=https://api-futures.kucoin.com
export API_KEY=key
export API_SECRET=secret
export API_PASSPHRASE=passphrase
export API_KEY_VERSION=2
export API_DEBUG_MODE=1

composer test

许可证

MIT