Okex API Like the official document interface, Support for arbitrary extension.

Overview

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.

Bitmex Support Websocket

Okex Support Websocket

Huobi Support Websocket

Binance Support Websocket

Kucoin

Mxc

Coinbase

ZB

Bitfinex

Bittrex

Kraken

Gate

Bigone

Crex24

Bybit

Coinbene

Bitget

Poloniex

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

Click to view

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

More use cases

More API

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

More Test

More API

Websocket

Websocket_v5 Click to view

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.

More Test

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);

More Test

You might also like...
Magento 2 Blog Extension is a better blog extension for Magento 2 platform. These include all useful features of Wordpress CMS
Magento 2 Blog Extension is a better blog extension for Magento 2 platform. These include all useful features of Wordpress CMS

Magento 2 Blog extension FREE Magento 2 Better Blog by Mageplaza is integrated right into the Magento backend so you can manage your blog and your e-c

PHPStan extension to support #[Readonly] constructor properties

icanhazstring/phpstan-readonly-property Support #[Readonly] promoted constructor properties for PHPStan. This library is used to have a full transitio

This Magento extension provides a Real Full Page Caching for Magento powered by Varnish with support of Session-Based information caching (Cart, Customer Accounts, ...) via ESI includes

This Magento extension provides a Real Full Page Caching (FPC) for Magento powered by Varnish with support of Session-Based information caching (Cart, Customer Accounts, ...) via ESI includes

The Assure Alliance support website. This website is based on Questions2Answers and is a forum for support using Biblical Tools

The Assure Alliance support website. This website is based on Questions2Answers and is a forum for support using Biblical Tools

A PHP API extension the 'An API of Ice and Fire'

This documentation is meant to enable you to consume our API and get you started right away using your favourite programming language. All endpoints and HTTP methods to be used have been detailed with a sample example for e

Zilliqa PHP is a typed PHP-7.1+ interface to Zilliqa JSON-RPC API.
Zilliqa PHP is a typed PHP-7.1+ interface to Zilliqa JSON-RPC API.

Zilliqa PHP is a typed PHP-7.1+ interface to Zilliqa JSON-RPC API. Check out the latest API documentation. Add library in a composer.json file.

This package provides a simple and intuitive way to work on the Youtube Data API. It provides fluent interface to Youtube features.

Laravel Youtube Client This package provides a simple and intuitive way to work on the Youtube Data API. It provides fluent interface to Youtube featu

vPOS Official Wordpres WooCommerce Plugin
vPOS Official Wordpres WooCommerce Plugin

vPOS - WooCommerce The number #1 payment solution in Angola This plugin currently works for the solutions listed below: EMIS GPO (Multicaixa Express)

This is the official place where Nemesysco's LVA7 and QA7 dockers will be hosted and maintained
This is the official place where Nemesysco's LVA7 and QA7 dockers will be hosted and maintained

LVA-Dockers This is the official place where Nemesysco's LVA7 and QA7 dockers will be hosted and maintained - It is still under construction! We are w

Comments
  • method_exists(): Argument #1 ($object_or_class) must be of type object|string, null given

    method_exists(): Argument #1 ($object_or_class) must be of type object|string, null given

    Hello, time to time I get this error "method_exists(): Argument #1 ($object_or_class) must be of type object|string, null given". Is seems in RequestException somehow fills with null. Here is screenshot mintty_zrZI0rywYI .

    opened by pavelforever 6
  • Option's underlying

    Option's underlying

    Hey, could you please tell, if there any way to get option instrument's underlying knowing only instrument id and instrument type in OKEX ? All API's that return underlying of instrument do require underlying in request :(

    opened by inommuidinov 0
Releases(2.2.3)
Owner
lin
Lifelong learner
lin
This document provides the details related to Remittance API. This APIs is used to initiate payment request from Mobile client/others exchange house.

City Bank Remittance API This is where your description should go. Limit it to a paragraph or two. Consider adding a small example. Installation You c

MD ARIFUL HAQUE 2 Oct 2, 2022
Smd thumbnail - Multiple image thumbnails of arbitrary dimensions

smd_thumbnail Download | Packagist If you’re bored of one Textpattern thumbnail per image and don’t fancy using an auto-resizing script or relying on

Stef Dawson 9 Dec 9, 2021
Base85 encoder and decoder for arbitrary data

All your Base85 Install Install with composer. $ composer require tuupola/base85 This branch requires PHP 7.1 or up. The older 1.x branch supports als

Mika Tuupola 25 Dec 2, 2022
Base62 encoder and decoder for arbitrary data

Base62 This library implements base62 encoding. In addition to integers it can encode and decode any arbitrary data. This is useful for example when g

Mika Tuupola 178 Nov 28, 2022
Official Kraken.io Magento Extension

Kraken.io Magento Extension Advanced optimization for your Magento JPEG, PNG, GIF and SVG images Established in 2012, Kraken.io is an industry-leading

Kraken.io Image Optimizer 21 Nov 30, 2022
Declaratively specify how to extract elements from a JSON document, in PHP

jmespath.php JMESPath (pronounced "jaymz path") allows you to declaratively specify how to extract elements from a JSON document. jmespath.php allows

null 1.7k Dec 30, 2022
A simple library for management the DOM (XML, HTML) document.

A simple library for management the DOM (XML, HTML) document.

Alexey 3 Oct 1, 2022
A PHP package for MRZ (Machine Readable Zones) code parser for Passport, Visa & Travel Document (TD1 & TD2).

MRZ (Machine Readable Zones) Parser for PHP A PHP package for MRZ (Machine Readable Zones) code parser for Passport, Visa & Travel Document (TD1 & TD2

Md. Rakibul Islam 25 Aug 24, 2022
Api.video-wordpress-plugin - The official api.video plugin for WordPress

api.video WordPress Plugin api.video is the video infrastructure for product builders. Lightning fast video APIs for integrating, scaling, and managin

api.video 5 Oct 19, 2022
Magento 2 Extension to cleanup admin menu and Store > Configuration area by arranging third party extension items.

Clean Admin Menu - Magento 2 Extension It will merge all 3rd party extension's menu items in backend's primary menu to a common menu item named "Exten

RedChamps 109 Jan 3, 2023