PHP code example of kucoin / kucoin-universal-sdk

1. Go to this page and download the library: Download kucoin/kucoin-universal-sdk library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

kucoin / kucoin-universal-sdk example snippets




uCoin\UniversalSDK\Api\DefaultClient;
use KuCoin\UniversalSDK\Model\ClientOptionBuilder;
use KuCoin\UniversalSDK\Model\Constants;
use KuCoin\UniversalSDK\Model\TransportOptionBuilder;

$transportOption = (new TransportOptionBuilder())
    ->setKeepAlive(true)
    ->setMaxConnections(10)
    ->build();

$clientOption = (new ClientOptionBuilder())
    ->setKey(getenv('API_KEY') ?: '')
    ->setSecret(getenv('API_SECRET') ?: '')
    ->setPassphrase(getenv('API_PASSPHRASE') ?: '')
    ->setSpotEndpoint(Constants::GLOBAL_API_ENDPOINT)
    ->setFuturesEndpoint(Constants::GLOBAL_FUTURES_API_ENDPOINT)
    ->setBrokerEndpoint(Constants::GLOBAL_BROKER_API_ENDPOINT)
    ->setTransportOption($transportOption)
    ->build();

$client = new DefaultClient($clientOption);
$accountApi = $client->restService()->getUTAService()->getAccountApi();

// GET /api/ua/v2/unified/account/overview
$response = $accountApi->getAccountOverview();
echo json_encode($response->data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . PHP_EOL;



uCoin\UniversalSDK\Api\DefaultClient;
use KuCoin\UniversalSDK\Model\ClientOptionBuilder;
use KuCoin\UniversalSDK\Model\Constants;
use KuCoin\UniversalSDK\Model\TransportOptionBuilder;
use KuCoin\UniversalSDK\Model\WebSocketClientOptionBuilder;
use React\EventLoop\Loop;

$loop = Loop::get();
$transportOption = (new TransportOptionBuilder())
    ->setKeepAlive(true)
    ->build();
$webSocketOption = (new WebSocketClientOptionBuilder())
    ->setReconnect(true)
    ->setReconnectAttempts(-1)
    ->setReconnectInterval(3.0)
    ->setDialTimeout(20.0)
    ->setWriteTimeout(10.0)
    ->build();

$clientOption = (new ClientOptionBuilder())
    ->setSpotEndpoint(Constants::GLOBAL_API_ENDPOINT)
    ->setFuturesEndpoint(Constants::GLOBAL_FUTURES_API_ENDPOINT)
    ->setBrokerEndpoint(Constants::GLOBAL_BROKER_API_ENDPOINT)
    ->setTransportOption($transportOption)
    ->setWebSocketClientOption($webSocketOption)
    ->build();

$client = new DefaultClient($clientOption, $loop);
$ticker = $client->wsService()->newUtaPublicWS('SPOT');

$ticker->start()
    ->then(function () use ($ticker) {
        return $ticker->ticker(['BTC-USDT'], function (array $event) {
            echo json_encode($event, JSON_UNESCAPED_SLASHES) . PHP_EOL;
        });
    })
    ->then(
        function (string $subscriptionId) {
            echo "Subscribed: {$subscriptionId}. Press Ctrl+C to stop." . PHP_EOL;
        },
        function ($error) use ($loop, $ticker) {
            fwrite(STDERR, 'UTA public WebSocket failed: ' . $error->getMessage() . PHP_EOL);
            $ticker->stop()->then(function () use ($loop) {
                $loop->stop();
            });
        }
    );

$loop->run();