<?php

$apiKey = 'ZwEzFeiqnVo356Our9';
$apiSecret = 'pKpGWLUSjwnXP3A2QXEhTuc5O30cSvXwoSMp';
$url = 'https://api.bybit.com/v5/account/wallet-balance';
$accountType = 'UNIFIED';
$coin = 'USDT';

// Create a timestamp (rounded to integer for correct format)
$timestamp = get_timestamp();

// Create the query parameters
$params = [
    'api_key' => $apiKey,
    'accountType' => $accountType,
    'coin' => $coin,
    'timestamp' => $timestamp
];

// Sort parameters before signing (some APIs require sorted query params for signature)
ksort($params);

// Create the query string
$queryString = http_build_query($params);

// Create the signature (sign the sorted query string + secret key)
$signature = hash('sha256', $queryString . $apiSecret);

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

// Create the full request URL
$requestUrl = $url . '?' . http_build_query($params);

echo $requestUrl . PHP_EOL;

// Initialize cURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $requestUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);  // Set a timeout to avoid waiting too long

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

// Check for errors
if ($response === false) {
    echo 'Curl error: ' . curl_error($ch);
} else {
    // Decode and print the response
    $decodedResponse = json_decode($response, true);
    echo '<pre>';
    print_r($decodedResponse);
    echo '</pre>';
}

// Close cURL
curl_close($ch);



function get_timestamp()
{
    $ch = curl_init();
    // Set the URL and other cURL options
    curl_setopt($ch, CURLOPT_URL, "https://api.bybit.com/v5/time");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    // Execute the request and get the response
    $response = curl_exec($ch);
    // Close the cURL session
    curl_close($ch);
    // Decode the JSON response
    $serverTime = json_decode($response, true);
    return $serverTime['serverTime'];

}


?>
