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\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);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.