Skip to content
This repository has been archived by the owner on Aug 12, 2024. It is now read-only.

Kucoin/kumex-php-sdk

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

76 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PHP SDK for KuMEX API

Note: This library isn't actively maintained. Please switch to kucoin-futures-php-sdk.

The detailed document https://docs.kucoin.com/futures/, in order to receive the latest API change notifications, please Watch this repository.

Latest Version PHP Version Build Status Total Downloads License

Requirements

Dependency Requirement
PHP >=5.5.0 Recommend PHP7+
guzzlehttp/guzzle ~6.0

Install

Install package via Composer.

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

Usage

Choose environment

Environment BaseUri
Production https://api-futures.kucoin.com (DEFAULT) https://api-futures.kucoin.io
Sandbox https://api-sandbox-futures.kucoin.com (DEFAULT) https://api-sandbox-futures.kucoin.io
// Switch to the sandbox environment
KuMEXApi::setBaseUri('https://api-sandbox-futures.kucoin.com');

Debug mode & logging

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

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

Examples

See the test case for more examples.

Example of API without authentication

use KuMEX\SDK\PublicApi\Time;

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

Example of API with authentication

use KuMEX\SDK\Auth;
use KuMEX\SDK\PrivateApi\Account;
use KuMEX\SDK\Exceptions\HttpException;
use KuMEX\SDK\Exceptions\BusinessException;

$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());
}

Example of WebSocket feed

use KuMEX\SDK\Auth;
use KuMEX\SDK\KuMEXApi;
use KuMEX\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 than curl.

pecl install swoole
composer require swlib/saber
use KuMEX\SDK\Auth;
use KuMEX\SDK\Http\SwooleHttp;
use KuMEX\SDK\KuMEXApi;
use KuMEX\SDK\PrivateApi\Order;
use KuMEX\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 = 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

KuMEX\SDK\PrivateApi\Account
API Authentication Description
KuMEX\SDK\PrivateApi\Account::getOverview() YES https://docs.kucoin.com/futures/#account
KuMEX\SDK\PrivateApi\Account::getTransactionHistory() YES https://docs.kucoin.com/futures/#get-transaction-history
KuMEX\SDK\PrivateApi\Account::transferIn() YES https://docs.kucoin.com/futures/#transfer-funds-from-kucoin-main-account-to-kumex-account
KuMEX\SDK\PrivateApi\Account::transferOut() YES https://docs.kucoin.com/futures/#transfer-funds-from-kumex-account-to-kucoin-main-account
KuMEX\SDK\PrivateApi\Account::transferOutV2() YES https://docs.kucoin.com/futures/#transfer-funds-from-kumex-account-to-kucoin-main-account
KuMEX\SDK\PrivateApi\Account::cancelTransferOut() YES https://docs.kucoin.com/futures/#cancel-transfer-out-request
KuMEX\SDK\PrivateApi\Account::getTransferList() YES https://docs.kucoin.com/futures/#get-transfer-out-request-records
KuMEX\SDK\PrivateApi\Deposit
API Authentication Description
KuMEX\SDK\PrivateApi\Deposit::getAddress() YES https://docs.kucoin.com/futures/#get-deposit-address
KuMEX\SDK\PrivateApi\Deposit::getDeposits() YES https://docs.kucoin.com/futures/#get-deposit-list
KuMEX\SDK\PrivateApi\Fill
API Authentication Description
KuMEX\SDK\PrivateApi\Fill::getFills() YES https://docs.kucoin.com/futures/#get-fills
KuMEX\SDK\PrivateApi\Fill::getRecentList() YES https://docs.kucoin.com/futures/#recent-fills
KuMEX\SDK\PrivateApi\Order
API Authentication Description
KuMEX\SDK\PrivateApi\Order::create() YES https://docs.kucoin.com/futures/#place-an-order
KuMEX\SDK\PrivateApi\Order::cancel() YES https://docs.kucoin.com/futures/#cancel-an-order
KuMEX\SDK\PrivateApi\Order::batchCancel() YES https://docs.kucoin.com/futures/#limit-order-mass-cancelation
KuMEX\SDK\PrivateApi\Order::stopOrders() YES https://docs.kucoin.com/futures/#stop-order-mass-cancelation
KuMEX\SDK\PrivateApi\Order::getList() YES https://docs.kucoin.com/futures/#get-order-list
KuMEX\SDK\PrivateApi\Order::getStopOrders() YES https://docs.kucoin.com/futures/#get-untriggered-stop-order-list
KuMEX\SDK\PrivateApi\Order::getRecentDoneOrders() YES https://docs.kucoin.com/futures/#get-list-of-orders-completed-in-24h
KuMEX\SDK\PrivateApi\Order::getDetail() YES https://docs.kucoin.com/futures/#get-details-of-a-single-order
KuMEX\SDK\PrivateApi\Order::getOpenOrderStatistics() YES https://docs.kucoin.com/futures/#active-order-value-calculation
KuMEX\SDK\PrivateApi\Position
API Authentication Description
KuMEX\SDK\PrivateApi\Position::getList() YES https://docs.kucoin.com/futures/#get-position-list
KuMEX\SDK\PrivateApi\Position::getDetail() YES https://docs.kucoin.com/futures/#get-position-details
KuMEX\SDK\PrivateApi\Position::changeAutoAppendStatus() YES https://docs.kucoin.com/futures/#enable-disable-of-auto-deposit-margin
KuMEX\SDK\PrivateApi\Position::marginAppend() YES https://docs.kucoin.com/futures/#add-margin-manually
KuMEX\SDK\PrivateApi\WebSocketFeed
API Authentication Description
KuMEX\SDK\PrivateApi\WebSocketFeed::getPublicServer() NO https://docs.kucoin.com/futures/#apply-connect-token
KuMEX\SDK\PrivateApi\WebSocketFeed::getPrivateServer() YES https://docs.kucoin.com/futures/#apply-connect-token
KuMEX\SDK\PrivateApi\WebSocketFeed::subscribePublicChannel() NO https://docs.kucoin.com/futures/#public-channels
KuMEX\SDK\PrivateApi\WebSocketFeed::subscribePublicChannels() NO https://docs.kucoin.com/futures/#public-channels
KuMEX\SDK\PrivateApi\WebSocketFeed::subscribePrivateChannel() YES https://docs.kucoin.com/futures/#private-channels
KuMEX\SDK\PrivateApi\WebSocketFeed::subscribePrivateChannels() YES https://docs.kucoin.com/futures/#private-channels
KuMEX\SDK\PrivateApi\Withdrawal
API Authentication Description
KuMEX\SDK\PrivateApi\Withdrawal::getQuotas() YES https://docs.kucoin.com/futures/#get-withdrawal-limit
KuMEX\SDK\PrivateApi\Withdrawal::getList() YES https://docs.kucoin.com/futures/#get-withdrawal-list
KuMEX\SDK\PrivateApi\Withdrawal::apply() YES https://docs.kucoin.com/futures/#withdraw-funds
KuMEX\SDK\PrivateApi\Withdrawal::cancel() YES https://docs.kucoin.com/futures/#cancel-withdrawal
KuMEX\SDK\PublicApi\Symbol
API Authentication Description
KuMEX\SDK\PublicApi\Symbol::getTicker() NO https://docs.kucoin.com/futures/#get-ticker
KuMEX\SDK\PublicApi\Symbol::getLevel2Snapshot() NO https://docs.kucoin.com/futures/#get-full-order-book-level-2
KuMEX\SDK\PublicApi\Symbol::getLevel3Snapshot() NO https://docs.kucoin.com/futures/#get-full-order-book-level-3
KuMEX\SDK\PublicApi\Symbol::getV2Level3Snapshot() NO https://docs.kucoin.com/futures/#get-full-order-book-level-3-v2
KuMEX\SDK\PublicApi\Symbol::getLevel2Message() NO https://docs.kucoin.com/futures/##level-2-pulling-messages
KuMEX\SDK\PublicApi\Symbol::getLevel3Message() NO https://docs.kucoin.com/futures/##level-3-pulling-messages
KuMEX\SDK\PublicApi\Symbol::getTradeHistory() NO https://docs.kucoin.com/futures/#get-trade-histories
KuMEX\SDK\PublicApi\Symbol::getKLines() NO https://docs.kucoin.com/futures/?lang=en_US#get-k-line-data-of-contract
KuMEX\SDK\PublicApi\Time
API Authentication Description
KuMEX\SDK\PublicApi\Time::timestamp() NO https://docs.kucoin.com/futures/#server-time
KuMEX\SDK\PublicApi\Status
API Authentication Description
KuMEX\SDK\PublicApi\Status::status() NO https://docs.kucoin.com/futures/#get-the-service-status

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-futures.kucoin.com
export API_KEY=key
export API_SECRET=secret
export API_PASSPHRASE=passphrase

composer test

License

MIT