It is recommended that you read the official document first
Okex docs https://www.okex.com/docs/en
Okex Simulation Test API https://www.okex.com/docs/en/#change-20200630,Click to view Demo
All interface methods are initialized the same as those provided by okex. See details src/api
Support Websocket v3 v5
Support V3 V5 API
Other exchanges API
Exchanges It includes all of the following exchanges and is highly recommended.
Installation
composer require linwj/okex
Support for more request Settings More
$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'=>[],
]);
Okex V5 API
Spot Trading API
Instrument related API More
use Lin\Okex\OkexSpot;
$okex=new OkexSpot();
//Getting the order book of a trading pair. Pagination is not supported here.
//The whole book will be returned for one request. WebSocket is recommended here.
try {
$result=$okex->instrument()->getBook([
'instrument_id'=>'BTC-USDT',
'size'=>20
]);
print_r($result);
}catch (\Exception $e){
print_r($e->getMessage());
}
//Get market data. This endpoint provides the snapshots of market data and can be used without verifications.
//List trading pairs and get the trading limit, price, and more information of different trading pairs.
try {
$result=$okex->instrument()->get();
print_r($result);
}catch (\Exception $e){
print_r($e->getMessage());
}
Order related API More
$okex=new OkexSpot($key,$secret,$passphrase);
//Place an Order
try {
$result=$okex->order()->post([
'instrument_id'=>'btc-usdt',
'side'=>'buy',
'price'=>'100',
'size'=>'0.001',
//'type'=>'market',
//'notional'=>'100'
]);
print_r($result);
}catch (\Exception $e){
print_r($e->getMessage());
}
sleep(1);
//Get order details by order ID.
try {
$result=$okex->order()->get([
'instrument_id'=>'btc-usdt',
'order_id'=>$result['order_id'],
]);
print_r($result);
}catch (\Exception $e){
print_r($e->getMessage());
}
sleep(1);
//Cancelling an unfilled order.
try {
$result=$okex->order()->postCancel([
'instrument_id'=>'btc-usdt',
'order_id'=>$result['order_id'],
]);
print_r($result);
}catch (\Exception $e){
print_r($e->getMessage());
}
Accounts related API More
$okex=new OkexSpot($key,$secret,$passphrase);
//This endpoint supports getting the list of assets(only show pairs with balance larger than 0),
//The balances, amount available/on hold in spot accounts.
try {
$result=$okex->account()->getAll();
print_r($result);
}catch (\Exception $e){
print_r($e->getMessage());
}
//This endpoint supports getting the balance, amount available/on hold of a token in spot account.
try {
$result=$okex->account()->get([
'currency'=>'BTC'
]);
print_r($result);
}catch (\Exception $e){
print_r($e->getMessage());
}
//All paginated requests return the latest information (newest) as the first page sorted by newest (in chronological time) first.
try {
$result=$okex->account()->getLedger([
'currency'=>'btc',
'limit'=>2,
]);
print_r($result);
}catch (\Exception $e){
print_r($e->getMessage());
}
Simulation Test API
$key = "09d4ed9e-6c2b-4652-9119-5c8eea078904";
$secret = "AE06CAA53CAB76CACDEE6001ACDABB11";
$passphrase = "test123";
$okex=new OkexSpot($key,$secret,$passphrase);
//You can set special needs
$okex->setOptions([
'timeout'=>10,
'headers'=>['x-simulated-trading'=>1],
]);
try {
$result=$okex->instrument()->get();
}catch (\Exception $e){
print_r(json_decode($e->getMessage(),true));
}
//Place an Order
try {
$result=$okex->order()->post([
'instrument_id'=>'MNBTC-MNUSDT',
'side'=>'buy',
'price'=>'100',
'size'=>'0.001',
//'type'=>'market',
//'notional'=>'100'
]);
print_r($result);
}catch (\Exception $e){
print_r(json_decode($e->getMessage(),true));
}
sleep(1);
//Get order details by order ID.
try {
$result=$okex->order()->get([
'instrument_id'=>'MNBTC-MNUSDT',
'order_id'=>$result['order_id'],
]);
print_r($result);
}catch (\Exception $e){
print_r(json_decode($e->getMessage(),true));
}
sleep(1);
//Cancelling an unfilled order.
try {
$result=$okex->order()->postCancel([
'instrument_id'=>'MNBTC-MNUSDT',
'order_id'=>$result['order_id'],
]);
print_r($result);
}catch (\Exception $e) {
print_r(json_decode($e->getMessage(), true));
}
Futures Trading API
Instrument related API More
$okex=new OkexFuture();
//List all contracts. This request does not support pagination. The full list will be returned for a request.
try {
$result=$okex->instrument()->getBook([
'instrument_id'=>'BTC-USD-190628',
'size'=>20,
]);
print_r($result);
}catch (\Exception $e){
print_r($e->getMessage());
}
//Get market data. This endpoint provides the snapshots of market data and can be used without verifications.
try {
$result=$okex->instrument()->get();
print_r($result);
}catch (\Exception $e){
print_r($e->getMessage());
}
Order related API More
$okex=new OkexFuture($key,$secret,$passphrase);
//Place an Order
try {
$result=$okex->order()->post([
'instrument_id'=>'btc-usd-190628',
'type'=>'1',
'price'=>'100',
'size'=>'1',
]);
print_r($result);
}catch (\Exception $e){
print_r($e->getMessage());
}
sleep(1);
//Get order details by order ID.
try {
$result=$okex->order()->get([
'instrument_id'=>'btc-usd-190628',
'order_id'=>$result['order_id'],
]);
print_r($result);
}catch (\Exception $e){
print_r($e->getMessage());
}
sleep(1);
//Cancelling an unfilled order.
try {
$result=$okex->order()->postCancel([
'instrument_id'=>'btc-usd-190628',
'order_id'=>$result['order_id'],
]);
print_r($result);
}catch (\Exception $e){
print_r($e->getMessage());
}
Accounts related API More
$okex=new OkexSpot($key,$secret,$passphrase);
//This endpoint supports getting the list of assets(only show pairs with balance larger than 0), the balances, amount available/on hold in spot accounts.
try {
$result=$okex->account()->getAll();
print_r($result);
}catch (\Exception $e){
print_r($e->getMessage());
}
//This endpoint supports getting the balance, amount available/on hold of a token in spot account.
try {
$result=$okex->account()->get([
'currency'=>'BTC'
]);
print_r($result);
}catch (\Exception $e){
print_r($e->getMessage());
}
//All paginated requests return the latest information (newest) as the first page sorted by newest (in chronological time) first.
try {
$result=$okex->account()->getLedger([
'currency'=>'btc',
'limit'=>2,
]);
print_r($result);
}catch (\Exception $e){
print_r($e->getMessage());
}
Position related API More
$okex=new OkexFuture($key,$secret,$passphrase);
//Get the information of holding positions of a contract.
try {
$result=$okex->position()->get([
'instrument_id'=>'BTC-USD-190628',
]);
print_r($result);
}catch (\Exception $e){
print_r($e->getMessage());
}
//Get the information of all holding positions in futures trading.Due to high energy consumption,
//You are advised to capture data with the "Futures Account of a Currency" API instead.
try {
$result=$okex->position()->getAll();
print_r($result);
}catch (\Exception $e){
print_r($e->getMessage());
}
Swap Trading API
Instrument related API More
$okex=new OkexFuture();
//List all contracts. This request does not support pagination. The full list will be returned for a request.
try {
$result=$okex->instrument()->getDepth([
'instrument_id'=>'BTC-USD-SWAP',
'size'=>10,
]);
print_r($result);
}catch (\Exception $e){
print_r($e->getMessage());
}
//Get market data. This endpoint provides the snapshots of market data and can be used without verifications.
try {
$result=$okex->instrument()->get();
print_r($result);
}catch (\Exception $e){
print_r($e->getMessage());
}
Order related API More
$okex=new OkexFuture($key,$secret,$passphrase);
//Place an Order
try {
$result=$okex->order()->post([
'instrument_id'=>'BTC-USD-SWAP',
'type'=>'1',
'price'=>'5000',
'size'=>'1',
]);
print_r($result);
}catch (\Exception $e){
print_r($e->getMessage());
}
sleep(1);
//Get order details by order ID.
try {
$result=$okex->order()->get([
'instrument_id'=>'BTC-USD-SWAP',
'order_id'=>$result['order_id'],
]);
print_r($result);
}catch (\Exception $e){
print_r($e->getMessage());
}
sleep(1);
//Cancelling an unfilled order.
try {
$result=$okex->order()->postCancel([
'instrument_id'=>'BTC-USD-SWAP',
'order_id'=>$result['order_id'],
]);
print_r($result);
}catch (\Exception $e){
print_r($e->getMessage());
}
Accounts related API More
$okex=new OkexSpot($key,$secret,$passphrase);
//This endpoint supports getting the list of assets(only show pairs with balance larger than 0), the balances, amount available/on hold in spot accounts.
try {
$result=$okex->account()->getAll();
print_r($result);
}catch (\Exception $e){
print_r($e->getMessage());
}
//This endpoint supports getting the balance, amount available/on hold of a token in spot account.
try {
$result=$okex->account()->get([
'instrument_id'=>'BTC-USD-SWAP'
]);
print_r($result);
}catch (\Exception $e){
print_r($e->getMessage());
}
//All paginated requests return the latest information (newest) as the first page sorted by newest (in chronological time) first.
try {
$result=$okex->account()->getLedger([
'instrument_id'=>'BTC-USD-SWAP',
'limit'=>2,
//'type'=>'1',
//'from'=>'',
//'to'=>'',
]);
print_r($result);
}catch (\Exception $e){
print_r($e->getMessage());
}
Position related API More
$okex=new OkexFuture($key,$secret,$passphrase);
//Get the information of holding positions of a contract.
try {
$result=$okex->position()->get([
'instrument_id'=>'BTC-USD-SWAP',
]);
print_r($result);
}catch (\Exception $e){
print_r($e->getMessage());
}
//Get the information of all holding positions in futures trading.Due to high energy consumption, you are advised to capture data with the "Futures Account of a Currency" API instead.
try {
$result=$okex->position()->getAll();
print_r($result);
}catch (\Exception $e){
print_r($e->getMessage());
}
Websocket
Websocket has two services, server and client. The server is responsible for dealing with the new connection of the exchange, data receiving, authentication and login. Client is responsible for obtaining and processing data.
Server initialization must be started in Linux CLI mode.
use Lin\Okex\OkexWebSocket;
require __DIR__ .'./vendor/autoload.php';
$okex=new OkexWebSocket();
$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();
If you want to test, you can "php server.php start" immediately outputs the log at the terminal.
If you want to deploy, you can "php server.php start -d" enables resident process mode, and enables "log=>true" to view logs.
Client side initialization.
$okex=new OkexWebSocket();
$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,
]);
Subscribe
//You can only subscribe to public channels
$okex->subscribe([
'spot/depth5:BCH-USDT',
'futures/depth5:BCH-USD-210326',
'swap/depth5:BCH-USD-SWAP',
'option/depth5:BTCUSD-20201021-11750-C',
]);
//You can also subscribe to both private and public channels
$okex->keysecret([
'key'=>'xxxxxxxxx',
'secret'=>'xxxxxxxxx',
'passphrase'=>'xxxxxxxxx',
]);
$okex->subscribe([
//public
'spot/depth5:BCH-USDT',
'futures/depth5:BCH-USD-210326',
'swap/depth5:BCH-USD-SWAP',
'option/depth5:BTCUSD-20201021-11750-C',
//private
'futures/position:BCH-USD-210326',
'futures/account:BCH-USDT',
'swap/position:BCH-USD-SWAP',
]);
unSubscribe
//Unsubscribe from public channels
$okex->unsubscribe([
'spot/depth5:BCH-USDT',
'futures/depth5:BCH-USD-210326',
'swap/depth5:BCH-USD-SWAP',
'option/depth5:BTCUSD-20201021-11750-C',
]);
//Unsubscribe from public and private channels
$okex->keysecret([
'key'=>'xxxxxxxxx',
'secret'=>'xxxxxxxxx',
'passphrase'=>'xxxxxxxxx',
]);
$okex->unsubscribe([
//public
'spot/depth5:BCH-USDT',
'futures/depth5:BCH-USD-210326',
'swap/depth5:BCH-USD-SWAP',
'option/depth5:BTCUSD-20201021-11750-C',
//private
'futures/position:BCH-USD-210326',
'futures/account:BCH-USDT',
'swap/position:BCH-USD-SWAP',
]);
Get all channel subscription data
//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);
Get partial channel subscription data
//The first way
$data=$okex->getSubscribe([
'spot/depth5:BCH-USDT',
'futures/depth5:BCH-USD-210326',
]);
print_r(json_encode($data));
//The second way callback
$okex->getSubscribe([
'spot/depth5:BCH-USDT',
'futures/depth5:BCH-USD-210326',
],function($data){
print_r(json_encode($data));
});
//The third way is to guard the process
$okex->getSubscribe([
'spot/depth5:BCH-USDT',
'futures/depth5:BCH-USD-210326',
],function($data){
print_r(json_encode($data));
},true);
Get partial private channel subscription data
//The first way
$okex->keysecret($key_secret);
$data=$okex->getSubscribe([
'futures/depth5:BCH-USD-210326',
'futures/position:BCH-USD-210326',//If there are private channels, $okex->keysecret() must be set
]);
print_r(json_encode($data));
//The second way callback
$okex->keysecret($key_secret);
$okex->getSubscribe([
'futures/depth5:BCH-USD-210326',
'futures/position:BCH-USD-210326',//If there are private channels, $okex->keysecret() must be set
],function($data){
print_r(json_encode($data));
});
//The third way is to guard the process
$okex->keysecret($key_secret);
$okex->getSubscribe([
'futures/depth5:BCH-USD-210326',
'futures/position:BCH-USD-210326',//If there are private channels, $okex->keysecret() must be set
],function($data){
print_r(json_encode($data));
},true);