<?php
$api_key = 'ZwEzFeiqnVo356Our9';
$secret_key = 'pKpGWLUSjwnXP3A2QXEhTuc5O30cSvXwoSMp';

//$api_key = 'XCdYb9znZdNDXpfdOh';
//$secret_key = 'Ts6NJ3BgIddv9BhSaFP2hkGogh3kh8j2uLzS';


$timestamp = round(microtime(true) * 1000); // Current timestamp in milliseconds

$params = [
      'api_key' => $api_key,
      'accountType' => 'UNIFIED',
      'category' => 'spot',
      'symbol' => 'SHIBUSDT', // Replace with your desired symbol
     'timestamp' => $timestamp,
];

// Sort parameters
ksort($params);

// Create query string
$query_string = http_build_query($params, '', '&', PHP_QUERY_RFC3986);

// Generate the signature
$sign = hash_hmac('sha256', $query_string, $secret_key);

// Add the sign to the parameters
$params['sign'] = $sign;

// Build the URL
$url = "https://api.bybit.com/v5/order/history?" . http_build_query($params);

// Initialize cURL session
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Execute the request
$response = curl_exec($ch);
curl_close($ch);

// Parse the response
$response_data = json_decode($response, true);

echo '<pre>';
print_r($response_data);
echo '</pre>';

// Check for success and extract order ID
if ($response_data['retCode'] == 0 && !empty($response_data['result']['list'])) {
    // Loop through each order and retrieve the order ID
    foreach ($response_data['result']['list'] as $order) {
        $orderId = $order['orderId'];
        echo "Order ID: " . $orderId . "\n";
    }
} else {
    echo "Error or no orders found: " . $response_data['retMsg'];
}
