PHP code example of linwj / bybit
1. Go to this page and download the library: Download linwj/bybit 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 / bybit example snippets
$bybit=new BybitV5($key,$secret);
//You can set special needs
$bybit->setOptions([
//Set the request timeout to 60 seconds by default
'timeout'=>10,
'headers'=>[
//X-Referer or Referer - 經紀商用戶專用的頭參數
//X-BAPI-RECV-WINDOW 默認值為5000
//cdn-request-id
'X-BAPI-RECV-WINDOW'=>'6000',
]
]);
try {
$result=$bybit->order()->postCreate([
'category'=>'spot',
'symbol'=>'BTCUSDT',
'side'=>'buy',
'orderType'=>'limit',
'qty'=>'1',
'price'=>'1000',
//'orderLinkId'=>'xxxxxxxxxxx',
]);
print_r($result);
}catch (\Exception $e){
print_r($e->getMessage());
}
try {
$result=$bybit->order()->getRealTime([
'category'=>'spot',
'symbol'=>'BTCUSDT',
'orderId'=>'xxxxxxxxxx',
//'orderLinkId'=>'xxxxxxxxxxx',
]);
print_r($result);
}catch (\Exception $e){
print_r($e->getMessage());
}
try {
$result=$bybit->order()->getSpotBorrowCheck([
'category'=>'spot',
'symbol'=>'BTCUSDT',
'side'=>'by'
]);
print_r($result);
}catch (\Exception $e){
print_r($e->getMessage());
}
$bybit=new BybitLinear();
//or new
//$bybit=new BybitInverse();
//You can set special needs
$bybit->setOptions([
//Set the request timeout to 60 seconds by default
'timeout'=>10,
//If you are developing locally and need an agent, you can set this
//'proxy'=>true,
//More flexible Settings
/* 'proxy'=>[
'http' => 'http://127.0.0.1:12333',
'https' => 'http://127.0.0.1:12333',
'no' => ['.cn']
], */
//Close the certificate
//'verify'=>false,
]);
$bybit=new BybitLinear();
try {
$result=$bybit->publics()->getOrderBookL2([
'symbol'=>'BTCUSDT'
]);
print_r($result);
}catch (\Exception $e){
print_r($e->getMessage());
}
try {
$result=$bybit->publics()->getKline([
'symbol'=>'BTCUSDT',
'interval'=>'15',
'from'=>time()-3600,
]);
print_r($result);
}catch (\Exception $e){
print_r($e->getMessage());
}
try {
$result=$bybit->publics()->getTickers();
print_r($result);
}catch (\Exception $e){
print_r($e->getMessage());
}
try {
$result=$bybit->publics()->getRecentTradingRecords([
'symbol'=>'BTCUSDT',
'limit'=>'5',
]);
print_r($result);
}catch (\Exception $e){
print_r($e->getMessage());
}
try {
$result=$bybit->publics()->getSymbols();
print_r($result);
}catch (\Exception $e){
print_r($e->getMessage());
}
$bybit=new BybitLinear($key,$secret);
try {
$result=$bybit->privates()->postOrderCreate([
//'order_link_id'=>'xxxxxxxxxxxxxx',
'side'=>'Buy',
'symbol'=>'BTCUSDT',
'order_type'=>'Limit',
'qty'=>'1',
'price'=>'4000',
'time_in_force'=>'GoodTillCancel',
'reduce_only'=>'false',
'close_on_trigger'=>'false',
]);
print_r($result);
}catch (\Exception $e){
print_r($e->getMessage());
}
try {
$result=$bybit->privates()->getOrderSearch([
'order_id'=>'xxxxxxxxxxxxx',
//'order_link_id'=>'xxxxxxxxxxxxxx',
'symbol'=>'BTCUSDT',
]);
print_r($result);
}catch (\Exception $e){
print_r($e->getMessage());
}
try {
$result=$bybit->privates()->postOrderReplace([
'order_id'=>'xxxxxxxxxxxxx',
'symbol'=>'BTCUSDT',
'p_r_qty'=>'2',
'p_r_price'=>'4999'
]);
print_r($result);
}catch (\Exception $e){
print_r($e->getMessage());
}
try {
$result=$bybit->privates()->postOrderCancel([
'order_id'=>'xxxxxxxxxxxxx',
//'order_link_id'=>'xxxxxxxxxxxxxx',
'symbol'=>'BTCUSDT',
]);
print_r($result);
}catch (\Exception $e){
print_r($e->getMessage());
}
try {
$result=$bybit->privates()->getOrderList([
'order_id'=>'xxxxxxxxxxxxx',
//'order_link_id'=>'xxxxxxxxxxxxxx',
'symbol'=>'BTCUSDT',
]);
print_r($result);
}catch (\Exception $e){
print_r($e->getMessage());
}
$bybit=new BybitLinear($key,$secret);
try {
$result=$bybit->privates()->postStopOrderCreate([
//'order_link_id'=>'xxxxxxxxxxxxxx',
'side'=>'Buy',
'symbol'=>'BTCUSDT',
'order_type'=>'Limit',
'qty'=>'1',
'price'=>'4000',
'time_in_force'=>'GoodTillCancel',
'reduce_only'=>'false',
'close_on_trigger'=>'false',
]);
print_r($result);
}catch (\Exception $e){
print_r($e->getMessage());
}
try {
$result=$bybit->privates()->getStopOrderSearch([
'order_id'=>'xxxxxxxxxxxxx',
//'order_link_id'=>'xxxxxxxxxxxxxx',
'symbol'=>'BTCUSDT',
]);
print_r($result);
}catch (\Exception $e){
print_r($e->getMessage());
}
try {
$result=$bybit->privates()->postStopOrderReplace([
'order_id'=>'xxxxxxxxxxxxx',
'symbol'=>'BTCUSDT',
'p_r_qty'=>'2',
'p_r_price'=>'4999'
]);
print_r($result);
}catch (\Exception $e){
print_r($e->getMessage());
}
try {
$result=$bybit->privates()->postStopOrderCancel([
'order_id'=>'xxxxxxxxxxxxx',
//'order_link_id'=>'xxxxxxxxxxxxxx',
'symbol'=>'BTCUSDT',
]);
print_r($result);
}catch (\Exception $e){
print_r($e->getMessage());
}
try {
$result=$bybit->privates()->getStopOrderList([
'order_id'=>'xxxxxxxxxxxxx',
//'order_link_id'=>'xxxxxxxxxxxxxx',
'symbol'=>'BTCUSDT',
]);
print_r($result);
}catch (\Exception $e){
print_r($e->getMessage());
}
$bybit=new BybitLinear($key,$secret);
try {
$result=$bybit->privates()->getPositionList([
'symbol'=>'BTCUSDT',
]);
print_r($result);
}catch (\Exception $e){
print_r($e->getMessage());
}
try {
$result=$bybit->privates()->postChangePositionMargin([
'symbol'=>'BTCUSDT',
'margin'=>'1'
]);
print_r($result);
}catch (\Exception $e){
print_r($e->getMessage());
}
try {
$result=$bybit->privates()->postPositionTradingStop([
'symbol'=>'BTCUSDT',
]);
print_r($result);
}catch (\Exception $e){
print_r($e->getMessage());
}
try {
$result=$bybit->privates()->getUserLeverage();
print_r($result);
}catch (\Exception $e){
print_r($e->getMessage());
}
try {
$result=$bybit->privates()->postUserLeverageSave([
'symbol'=>'BTCUSDT',
'leverage'=>'1'
]);
print_r($result);
}catch (\Exception $e){
print_r($e->getMessage());
}
try {
$result=$bybit->privates()->getExecutionList([
'symbol'=>'BTCUSDT',
]);
print_r($result);
}catch (\Exception $e){
print_r($e->getMessage());
}
$bybit=new BybitInverse();
try {
$result=$bybit->publics()->getOrderBookL2([
'symbol'=>'BTCUSD'
]);
print_r($result);
}catch (\Exception $e){
print_r($e->getMessage());
}
try {
$result=$bybit->publics()->getKlineList([
'symbol'=>'BTCUSD',
'interval'=>'15',
'from'=>time()-3600,
]);
print_r($result);
}catch (\Exception $e){
print_r($e->getMessage());
}
try {
$result=$bybit->publics()->getTickers();
print_r($result);
}catch (\Exception $e){
print_r($e->getMessage());
}
try {
$result=$bybit->publics()->getTradingRecords([
'symbol'=>'BTCUSD',
'limit'=>'5',
]);
print_r($result);
}catch (\Exception $e){
print_r($e->getMessage());
}
try {
$result=$bybit->publics()->getSymbols();
print_r($result);
}catch (\Exception $e){
print_r($e->getMessage());
}
$bybit=new BybitInverse($key,$secret);
try {
$result=$bybit->privates()->postOrderCreate([
//'order_link_id'=>'xxxxxxxxxxxxxx',
'side'=>'Buy',
'symbol'=>'BTCUSD',
'order_type'=>'Limit',
'qty'=>'1',
'price'=>'4000',
'time_in_force'=>'GoodTillCancel',
]);
print_r($result);
}catch (\Exception $e){
print_r($e->getMessage());
}
try {
$result=$bybit->privates()->getOrder([
'order_id'=>'xxxxxxxxxxxxx',
//'order_link_id'=>'xxxxxxxxxxxxxx',
'symbol'=>'BTCUSD',
]);
print_r($result);
}catch (\Exception $e){
print_r($e->getMessage());
}
try {
$result=$bybit->privates()->postOrderReplace([
'order_id'=>'xxxxxxxxxxxxx',
'symbol'=>'BTCUSD',
'p_r_qty'=>'2',
'p_r_price'=>'4999'
]);
print_r($result);
}catch (\Exception $e){
print_r($e->getMessage());
}
try {
$result=$bybit->privates()->postOrderCancel([
'order_id'=>'xxxxxxxxxxxxx',
//'order_link_id'=>'xxxxxxxxxxxxxx',
'symbol'=>'BTCUSD',
]);
print_r($result);
}catch (\Exception $e){
print_r($e->getMessage());
}
try {
$result=$bybit->privates()->getOrderList([
'order_id'=>'xxxxxxxxxxxxx',
//'order_link_id'=>'xxxxxxxxxxxxxx',
'symbol'=>'BTCUSD',
]);
print_r($result);
}catch (\Exception $e){
print_r($e->getMessage());
}
$bybit=new BybitInverse($key,$secret);
try {
$result=$bybit->privates()->postStopOrderCreate([
//'order_link_id'=>'xxxxxxxxxxxxxx',
'side'=>'Buy',
'symbol'=>'BTCUSD',
'order_type'=>'Limit',
'qty'=>'1',
'price'=>'4000',
'time_in_force'=>'GoodTillCancel',
]);
print_r($result);
}catch (\Exception $e){
print_r($e->getMessage());
}
try {
$result=$bybit->privates()->getStopOrder([
'order_id'=>'xxxxxxxxxxxxx',
//'order_link_id'=>'xxxxxxxxxxxxxx',
'symbol'=>'BTCUSD',
]);
print_r($result);
}catch (\Exception $e){
print_r($e->getMessage());
}
try {
$result=$bybit->privates()->postStopOrderReplace([
'order_id'=>'xxxxxxxxxxxxx',
'symbol'=>'BTCUSD',
'p_r_qty'=>'2',
'p_r_price'=>'4999'
]);
print_r($result);
}catch (\Exception $e){
print_r($e->getMessage());
}
try {
$result=$bybit->privates()->postStopOrderCancel([
'order_id'=>'xxxxxxxxxxxxx',
//'order_link_id'=>'xxxxxxxxxxxxxx',
'symbol'=>'BTCUSD',
]);
print_r($result);
}catch (\Exception $e){
print_r($e->getMessage());
}
try {
$result=$bybit->privates()->getStopOrderList([
'order_id'=>'xxxxxxxxxxxxx',
//'order_link_id'=>'xxxxxxxxxxxxxx',
'symbol'=>'BTCUSD',
]);
print_r($result);
}catch (\Exception $e){
print_r($e->getMessage());
}
$bybit=new BybitInverse($key,$secret);
try {
$result=$bybit->privates()->getPositionList([
'symbol'=>'BTCUSD',
]);
print_r($result);
}catch (\Exception $e){
print_r($e->getMessage());
}
try {
$result=$bybit->privates()->postChangePositionMargin([
'symbol'=>'BTCUSD',
'margin'=>'1'
]);
print_r($result);
}catch (\Exception $e){
print_r($e->getMessage());
}
try {
$result=$bybit->privates()->postPositionTradingStop([
'symbol'=>'BTCUSD',
]);
print_r($result);
}catch (\Exception $e){
print_r($e->getMessage());
}
try {
$result=$bybit->privates()->getUserLeverage();
print_r($result);
}catch (\Exception $e){
print_r($e->getMessage());
}
try {
$result=$bybit->privates()->postUserLeverageSave([
'symbol'=>'BTCUSD',
'leverage'=>'1'
]);
print_r($result);
}catch (\Exception $e){
print_r($e->getMessage());
}
try {
$result=$bybit->privates()->getExecutionList([
'symbol'=>'BTCUSD',
]);
print_r($result);
}catch (\Exception $e){
print_r($e->getMessage());
}