PHP code example of linwj / okex

1. Go to this page and download the library: Download linwj/okex 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/ */

    

linwj / okex example snippets


$okex=new OkexSpot();
//or
$okex=new OkexSpot($key,$secret,$passphrase);

//You can set special needs
$okex->setOptions([
    //Set the request timeout to 60 seconds by default
    'timeout'=>10,
    //https://github.com/guzzle/guzzle
    'proxy'=>[],
    //https://www.php.net/manual/en/book.curl.php
    'curl'=>[],
    
    //Set Demo Trading
    'headers'=>['x-simulated-trading'=>1]
]);

use Lin\Okex\OkexV5;
$okex=new OkexV5();

try {
    $result=$okex->market()->getTickers([
        'instType'=>'SPOT',
    ]);
    print_r($result);
}catch (\Exception $e){
    print_r(json_decode($e->getMessage(),true));
}

try {
    $result=$okex->market()->getTicker([
        'instId'=>'BTC-USD-SWAP',
    ]);
    print_r($result);
}catch (\Exception $e){
    print_r(json_decode($e->getMessage(),true));
}

try {
    $result=$okex->market()->getIndexTickers([
        'instId'=>'BTC-USD',
    ]);
    print_r($result);
}catch (\Exception $e){
    print_r(json_decode($e->getMessage(),true));
}

try {
    $result=$okex->market()->getCandles([
        'instId'=>'BTC-USD',
    ]);
    print_r($result);
}catch (\Exception $e){
    print_r(json_decode($e->getMessage(),true));
}

try {
    $result=$okex->market()->getIndexCandles([
        'instId'=>'BTC-USD',
    ]);
    print_r($result);
}catch (\Exception $e){
    print_r(json_decode($e->getMessage(),true));
}

use Lin\Okex\OkexV5;

$okex=new OkexV5($key,$secret,$passphrase);

try {
    $result=$okex->trade()->postOrder([
        'instId'=>'BTC-USDT',
        'tdMode'=>'cross',
        'clOrdId'=>'xxxxxxxxxxx',
        'side'=>'buy',
        'ordType'=>'limit',
        'sz'=>'0.01',
        'px'=>'10000',
    ]);
    print_r($result);
}catch (\Exception $e){
    print_r(json_decode($e->getMessage(),true));
}

try {
    $result=$okex->trade()->postCancelOrder([
        'instId'=>'BTC-USDT',
        'ordId'=>'xxxxxxxxx',
        //'clOrdId'=>'xxxxxxxxxxx',
    ]);
    print_r($result);
}catch (\Exception $e){
    print_r(json_decode($e->getMessage(),true));
}


try {
    $result=$okex->trade()->postAmendOrder([
        'instId'=>'BTC-USDT',
        'ordId'=>'xxxxxxxxx',
        //'clOrdId'=>'xxxxxxxxxxx',
        'newSz'=>'0.012',
        'newPx'=>'11000',
    ]);
    print_r($result);
}catch (\Exception $e){
    print_r(json_decode($e->getMessage(),true));
}

try {
    $result=$okex->trade()->getOrder([
        'instId'=>'BTC-USDT',
        'ordId'=>'xxxxxxxxx',
        //'clOrdId'=>'xxxxxxxxxxx',
    ]);
    print_r($result);
}catch (\Exception $e){
    print_r(json_decode($e->getMessage(),true));
}


try {
    $result=$okex->trade()->postOrderAlgo([
        'instId'=>'BTC-USDT',
        'tdMode'=>'cross',
        'clOrdId'=>'xxxxxxxxxxx',
        'side'=>'buy',
        'ordType'=>'trigger',
        'sz'=>'0.01',
    ]);
    print_r($result);
}catch (\Exception $e){
    print_r(json_decode($e->getMessage(),true));
}


use Lin\Okex\OkexV5;

$okex=new OkexV5($key,$secret,$passphrase);

try {
    $result=$okex->account()->getBalance();
    print_r($result);
}catch (\Exception $e){
    print_r(json_decode($e->getMessage(),true));
}

try {
    $result=$okex->account()->getPositions();
    print_r($result);
}catch (\Exception $e){
    print_r(json_decode($e->getMessage(),true));
}

try {
    $result=$okex->account()->getBills();
    print_r($result);
}catch (\Exception $e){
    print_r(json_decode($e->getMessage(),true));
}

try {
    $result=$okex->account()->getBillsArchive();
    print_r($result);
}catch (\Exception $e){
    print_r(json_decode($e->getMessage(),true));
}

try {
    $result=$okex->account()->getConfig();
    print_r($result);
}catch (\Exception $e){
    print_r(json_decode($e->getMessage(),true));
}

try {
    $result=$okex->account()->postSetPositionMode([
        'posMode'=>'long_short_mode'
    ]);
    print_r($result);
}catch (\Exception $e){
    print_r(json_decode($e->getMessage(),true));
}

try {
    $result=$okex->account()->postSetLeverage([
        'instId'=>'BTC-USDT',
        //'ccy'=>'',
        'lever'=>'5',
        'mgnMode'=>'cross',
    ]);
    print_r($result);
}catch (\Exception $e){
    print_r(json_decode($e->getMessage(),true));
}

try {
    $result=$okex->account()->getMaxSize([
        'instId'=>'BTC-USDT',
        'tdMode'=>'cross',
    ]);
    print_r($result);
}catch (\Exception $e){
    print_r(json_decode($e->getMessage(),true));
}

use Lin\Okex\OkexWebSocketV5;


$okex->config([
    //Do you want to enable local logging,default false
    'log'=>true,

    //Daemons address and port,default 0.0.0.0:2207
    //'global'=>'127.0.0.1:2208',

    //Heartbeat time,default 20 seconds
    //'ping_time'=>20,

    //Channel subscription monitoring time,2 seconds
    //'listen_time'=>2,

    //Channel data update time,0.1 seconds
    //'data_time'=>0.1,

    //Number of messages WS queue shuold hold, default 100
    //'queue_count'=>100,
]);

$okex->start();

$okex=new OkexWebSocketV5();

$okex->config([
    //Do you want to enable local logging,default false
    'log'=>true,
    //Or set the log name
    //'log'=>['filename'=>'okex'],

    //Daemons address and port,default 0.0.0.0:2207
    //'global'=>'127.0.0.1:2208',

    //Heartbeat time,default 20 seconds
    //'ping_time'=>20,

    //Channel subscription monitoring time,2 seconds
    //'listen_time'=>2,

    //Channel data update time,0.1 seconds
    //'data_time'=>0.1,

    //Number of messages WS queue shuold hold, default 100
    //'queue_count'=>100,
]);

//You can only subscribe to public channels
$okex->subscribe([
    ["channel"=>"instruments","instType"=>"SPOT"],
    ["channel"=>"instruments","instType"=>"SWAP"],
    ["channel"=>"instruments","instType"=>"FUTURES"],
    ["channel"=>"instruments","instType"=>"OPTION"],
]);

//You can also subscribe to both private and public channels
$okex->keysecret([
    'key'=>'xxxxxxxxx',
    'secret'=>'xxxxxxxxx',
    'passphrase'=>'xxxxxxxxx',
]);
$okex->subscribe([
    //public
    ["channel"=>"tickers","instId"=>"BTC-USDT"],
    ["channel"=>"tickers","instId"=>"BTC-USD-SWAP"],
    ["channel"=>"tickers","instId"=>"BTC-USD-210924"],

    ["channel"=>"books","instId"=>"BTC-USDT"],
    ["channel"=>"books","instId"=>"BTC-USD-SWAP"],
    ["channel"=>"books","instId"=>"BTC-USD-210924"],

    ["channel"=>"candle5m","instId"=>"BTC-USDT"],
    ["channel"=>"candle15m","instId"=>"BTC-USD-SWAP"],
    ["channel"=>"candle30m","instId"=>"BTC-USD-210924"],

    //private
    ["channel"=>"account","ccy"=>"BTC"],
    ["channel"=>"positions","instType"=>"FUTURES","uly"=>"BTC-USD","instId"=>"BTC-USD-210924"],
    ["channel"=>"balance_and_position"],
    ["channel"=>"orders","instType"=>"FUTURES","uly"=>"BTC-USD","instId"=>"BTC-USD-210924"],
    ["channel"=>"orders-algo","instType"=>"FUTURES","uly"=>"BTC-USD","instId"=>"BTC-USD-210924"],
]);

//Unsubscribe from public channels
$okex->unsubscribe([
    ["channel"=>"instruments","instType"=>"SPOT"],
    ["channel"=>"instruments","instType"=>"SWAP"],
    ["channel"=>"instruments","instType"=>"FUTURES"],
    ["channel"=>"instruments","instType"=>"OPTION"],
]);

//Unsubscribe from public and private channels
$okex->keysecret([
    'key'=>'xxxxxxxxx',
    'secret'=>'xxxxxxxxx',
    'passphrase'=>'xxxxxxxxx',
]);
$okex->unsubscribe([
    //public
    ["channel"=>"tickers","instId"=>"BTC-USDT"],
    ["channel"=>"tickers","instId"=>"BTC-USD-SWAP"],
    ["channel"=>"tickers","instId"=>"BTC-USD-210924"],

    ["channel"=>"books","instId"=>"BTC-USDT"],
    ["channel"=>"books","instId"=>"BTC-USD-SWAP"],
    ["channel"=>"books","instId"=>"BTC-USD-210924"],

    ["channel"=>"candle5m","instId"=>"BTC-USDT"],
    ["channel"=>"candle15m","instId"=>"BTC-USD-SWAP"],
    ["channel"=>"candle30m","instId"=>"BTC-USD-210924"],

    //private
    ["channel"=>"account","ccy"=>"BTC"],
    ["channel"=>"positions","instType"=>"FUTURES","uly"=>"BTC-USD","instId"=>"BTC-USD-210924"],
    ["channel"=>"balance_and_position"],
    ["channel"=>"orders","instType"=>"FUTURES","uly"=>"BTC-USD","instId"=>"BTC-USD-210924"],
    ["channel"=>"orders-algo","instType"=>"FUTURES","uly"=>"BTC-USD","instId"=>"BTC-USD-210924"],
]);


//The first way
$data=$okex->getSubscribe();
print_r(json_encode($data));

//The second way callback
$okex->getSubscribe(function($data){
    print_r(json_encode($data));
});

//The third way is to guard the process
$okex->getSubscribe(function($data){
    print_r(json_encode($data));
},true);

//The first way
$data=$okex->getSubscribe([
    ["channel"=>"tickers","instId"=>"BTC-USDT"],
    ["channel"=>"tickers","instId"=>"BTC-USD-SWAP"],
    ["channel"=>"tickers","instId"=>"BTC-USD-210924"],
]);
print_r(json_encode($data));

//The second way callback
$okex->getSubscribe([
    ["channel"=>"tickers","instId"=>"BTC-USDT"],
    ["channel"=>"tickers","instId"=>"BTC-USD-SWAP"],
    ["channel"=>"tickers","instId"=>"BTC-USD-210924"],
],function($data){
    print_r(json_encode($data));
});

//The third way is to guard the process
$okex->getSubscribe([
    ["channel"=>"tickers","instId"=>"BTC-USDT"],
    ["channel"=>"tickers","instId"=>"BTC-USD-SWAP"],
    ["channel"=>"tickers","instId"=>"BTC-USD-210924"],
],function($data){
    print_r(json_encode($data));
},true);

//The first way
$okex->keysecret($key_secret);
$data=$okex->getSubscribe([
    ["channel"=>"books","instId"=>"BTC-USDT"],
    ["channel"=>"books","instId"=>"BTC-USD-SWAP"],

    ["channel"=>"account","ccy"=>"BTC"],
    ["channel"=>"positions","instType"=>"FUTURES","uly"=>"BTC-USD","instId"=>"BTC-USD-210924"],
]);
print_r(json_encode($data));

//The second way callback
$okex->keysecret($key_secret);
$okex->getSubscribe([
    ["channel"=>"books","instId"=>"BTC-USDT"],
    ["channel"=>"books","instId"=>"BTC-USD-SWAP"],

    ["channel"=>"account","ccy"=>"BTC"],
    ["channel"=>"positions","instType"=>"FUTURES","uly"=>"BTC-USD","instId"=>"BTC-USD-210924"],
],function($data){
    print_r(json_encode($data));
});

//The third way is to guard the process
$okex->keysecret($key_secret);
$okex->getSubscribe([
    ["channel"=>"books","instId"=>"BTC-USDT"],
    ["channel"=>"books","instId"=>"BTC-USD-SWAP"],

    ["channel"=>"account","ccy"=>"BTC"],
    ["channel"=>"positions","instType"=>"FUTURES","uly"=>"BTC-USD","instId"=>"BTC-USD-210924"],
],function($data){
    print_r(json_encode($data));
},true);

$okex->reconPublic();

$okex->reconPrivate($key);