PHP code example of tigusigalpa / bitget-php

1. Go to this page and download the library: Download tigusigalpa/bitget-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/ */

    

tigusigalpa / bitget-php example snippets


use Tigusigalpa\Bitget\Facades\Bitget;

$tickers = Bitget::market()->getTickers('SPOT', 'BTCUSDT');

use Tigusigalpa\Bitget\Client;

$client = new Client(
    apiKey: getenv('BITGET_API_KEY'),
    secretKey: getenv('BITGET_SECRET_KEY'),
    passphrase: getenv('BITGET_PASSPHRASE'),
);

$tickers = $client->market()->getTickers('SPOT', 'BTCUSDT');

use Tigusigalpa\Bitget\WebsocketClient;

$ws = new WebsocketClient(WebsocketClient::DEFAULT_PUBLIC_URL);
$ws->connect();
$ws->subscribe(['instType' => 'SPOT', 'topic' => 'ticker', 'symbol' => 'BTCUSDT']);

$ws->listen(function (array $push) {
    echo json_encode($push), PHP_EOL;
});

$ws = new WebsocketClient(
    url: WebsocketClient::DEFAULT_PRIVATE_URL,
    apiKey: config('bitget.api_key'),
    secretKey: config('bitget.secret_key'),
    passphrase: config('bitget.passphrase'),
);
$ws->connect();
$ws->subscribe(['instType' => 'UTA', 'topic' => 'fast-fill', 'symbol' => 'default']);
$ws->listen(fn (array $push) => /* handle fill */ null);

$client = new Client(
    apiKey: config('bitget.api_key'),
    secretKey: config('bitget.secret_key'),
    passphrase: config('bitget.passphrase'),
    demoTrading: true,
);

// Guard order placement behind an explicit opt-in — never wire this to
// production credentials without removing the gate deliberately.
if (getenv('BITGET_ENABLE_TRADING') === '1') {
    $client->trade()->placeOrder([
        'category' => 'SPOT',
        'symbol' => 'BTCUSDT',
        'side' => 'buy',
        'orderType' => 'limit',
        'price' => '10000', // deliberately far below market so it won't fill
        'qty' => '0.001',
    ]);
}

use Tigusigalpa\Bitget\Exceptions\AuthenticationException;
use Tigusigalpa\Bitget\Exceptions\InsufficientFundsException;
use Tigusigalpa\Bitget\Exceptions\RateLimitException;
use Tigusigalpa\Bitget\Exceptions\BitgetException;

try {
    $client->trade()->placeOrder([...]);
} catch (AuthenticationException $e) {
    // invalid API key/secret/passphrase
} catch (InsufficientFundsException $e) {
    // not enough balance/margin
} catch (RateLimitException $e) {
    // back off and retry
} catch (BitgetException $e) {
    // anything else; $e->bitgetCode / $e->rawResponse are available
}
bash
composer 
bash
php artisan vendor:publish --tag=bitget-config