rabsana / kucoin-php-sdk
PHP SDK for KuCoin API
v1.1.0
2023-08-31 09:12 UTC
Requires
- php: >=5.5.0
- ext-json: *
- guzzlehttp/guzzle: ^6.0|^7.0
- monolog/monolog: ~1.0|~2.0
- ratchet/pawl: ^0.3.2
Requires (Dev)
- phpunit/phpunit: ^5.7
README
The detailed document https://docs.kucoin.com, in order to receive the latest API change notifications, please
Watch
this repository.
Requirements
Install
Install package via Composer.
composer require "kucoin/kucoin-php-sdk:~1.1.0"
Usage
Choose environment
// Switch to the sandbox environment KuCoinApi::setBaseUri('https://openapi-sandbox.kucoin.com');
Debug mode & logging
// 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");
Examples
See the test case for more examples.
Example of API without
authentication
use KuCoin\SDK\PublicApi\Time; $api = new Time(); $timestamp = $api->timestamp(); var_dump($timestamp);
Note
To reinforce the security of the API, KuCoin upgraded the API key to version 2.0, the validation logic has also been changed. It is recommended to create(https://www.kucoin.com/account/api) and update your API key to version 2.0. The API key of version 1.0 will be still valid until May 1, 2021
Example of API with
authentication
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()); }
Example of WebSocket feed
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); // 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"; });
⚡️Coroutine HTTP client for asynchronous IO
See the benchmark, almost
20x
faster thancurl
.
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 list
KuCoin\SDK\PrivateApi\Account
KuCoin\SDK\PrivateApi\Deposit
KuCoin\SDK\PrivateApi\TradeFee
KuCoin\SDK\PrivateApi\Symbol
KuCoin\SDK\PrivateApi\Order
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\PublicApi\Time
KuCoin\SDK\PublicApi\ServiceStatus
Run tests
Modify your API key in
phpunit.xml
first.
# 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