1. Go to this page and download the library: Download tigusigalpa/bybit-php 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/ */
// In .env file
BYBIT_DEMO_TRADING=true
// Use normally
$balance = Bybit::getWalletBalance(['accountType' => 'UNIFIED']);
use Tigusigalpa\ByBit\BybitWebSocket;
// Create WebSocket instance
$ws = new BybitWebSocket(
apiKey: null,
apiSecret: null,
testnet: false,
region: 'global',
isPrivate: false
);
// Subscribe to orderbook
$ws->subscribeOrderbook('BTCUSDT', 50);
// Subscribe to trades
$ws->subscribeTrade('BTCUSDT');
// Subscribe to ticker
$ws->subscribeTicker('BTCUSDT');
// Subscribe to klines
$ws->subscribeKline('BTCUSDT', '1'); // 1m candles
// Handle messages
$ws->onMessage(function($data) {
if (isset($data['topic'])) {
echo "Topic: {$data['topic']}\n";
print_r($data['data']);
}
});
// Start listening (blocking)
$ws->listen();
use Tigusigalpa\ByBit\BybitWebSocket;
$ws = new BybitWebSocket(
apiKey: 'your_api_key',
apiSecret: 'your_api_secret',
testnet: false,
region: 'global',
isPrivate: true
);
// Subscribe to position updates
$ws->subscribePosition();
// Subscribe to order updates
$ws->subscribeOrder();
// Subscribe to execution updates
$ws->subscribeExecution();
// Subscribe to wallet updates
$ws->subscribeWallet();
$ws->onMessage(function($data) {
match($data['topic'] ?? null) {
'position' => handlePositionUpdate($data),
'order' => handleOrderUpdate($data),
'execution' => handleExecutionUpdate($data),
'wallet' => handleWalletUpdate($data),
default => null
};
});
$ws->listen();
// app/Console/Commands/BybitWebSocketListener.php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Tigusigalpa\ByBit\BybitWebSocket;
class BybitWebSocketListener extends Command
{
protected $signature = 'bybit:listen {symbol=BTCUSDT}';
protected $description = 'Listen to Bybit WebSocket streams';
public function handle()
{
$symbol = $this->argument('symbol');
$ws = app(BybitWebSocket::class);
$ws->subscribeOrderbook($symbol, 50);
$ws->subscribeTrade($symbol);
$ws->onMessage(fn($data) =>
$this->info(json_encode($data, JSON_PRETTY_PRINT))
);
$this->info("🚀 WebSocket listener started for {$symbol}...");
$ws->listen();
}
}
use Tigusigalpa\ByBit\BybitClient;
use Tigusigalpa\ByBit\BybitTradFi;
$client = new BybitClient(
apiKey: 'your_api_key',
apiSecret: 'your_api_secret',
testnet: true
);
$tradfi = new BybitTradFi($client);
// Get instruments, optionally filtered by asset class:
// 'metal', 'forex', 'stock', 'index', 'commodity', or '' for all
$instruments = $tradfi->getInstruments('forex');
// Single ticker
$gold = $tradfi->getTicker('XAUUSD');
echo $gold['result']['list'][0]['lastPrice'];
// Shortcut tickers
$metals = $tradfi->getMetalsTickers(); // XAUUSD, XAGUSD, XPTUSD
$forex = $tradfi->getForexTickers(); // major pairs
$stocks = $tradfi->getStockTickers(); // US stock CFDs
$indices = $tradfi->getIndexTickers(); // US500USD, DE40USD, etc.
// Kline / candlestick data
// interval: 1, 3, 5, 15, 30, 60, 120, 240, 360, 720, D, W, M
$klines = $tradfi->getKline('XAUUSD', '60', 50);
// Order book depth (1, 25, 50, 100, 200)
$orderbook = $tradfi->getOrderbook('EURUSD', 25);
// Overnight swap fee info
$swap = $tradfi->getSwapFee('XAUUSD');
// Trading fee rate
$fee = $tradfi->getFeeRate('EURUSD');
// Place a limit buy order on gold with TP/SL
$order = $tradfi->placeOrder(
symbol: 'XAUUSD',
side: 'Buy',
orderType: 'Limit',
qty: '0.01',
price: '3200',
extra: [
'timeInForce' => 'GTC',
'takeProfit' => '3350',
'stopLoss' => '3100',
]
);
// Place a market sell on EURUSD
$order = $tradfi->placeOrder('EURUSD', 'Sell', 'Market', '1');
// Close an open long position at market price
$tradfi->closePosition('XAUUSD', 'Buy', '0.01');
// Set leverage
$tradfi->setLeverage('XAUUSD', 10);
// Cancel an order
$tradfi->cancelOrder('XAUUSD', orderId: 'abc123');
// Open orders
$orders = $tradfi->getOpenOrders('XAUUSD');
// Order & trade history
$history = $tradfi->getOrderHistory('EURUSD', limit: 50);
$trades = $tradfi->getTradeHistory('XAUUSD', limit: 50);
// Open positions (pass '' for all TradFi positions)
$positions = $tradfi->getPositions('XAUUSD');
BybitTradFi::isTradFiSymbol('XAUUSD'); // true — gold
BybitTradFi::isTradFiSymbol('EURUSD'); // true — forex
BybitTradFi::isTradFiSymbol('US500USD'); // true — index
BybitTradFi::isTradFiSymbol('BTCUSDT'); // false — crypto
BybitTradFi::isTradFiSymbol('ETHUSDT'); // false — crypto
// Filter TradFi positions from a mixed position list
$allPositions = $client->getPositions(['category' => 'linear']);
$tradfiOnly = array_filter(
$allPositions['result']['list'] ?? [],
fn($p) => BybitTradFi::isTradFiSymbol($p['symbol'])
);