<?php
$apiKey = 'nmBG3nleatbKhe7oAmrVdNEjFnQCAHWWVmhkSqIWSzG4MsCce97CKo4Y1RiEIGu7';
$apiSecret = '4BjGHQWLXQH6y4ZbGtbpgu9NT3corQrcIW9CNZwo2fW0dqY2ImtRCIsYAfIt9Q0k';
$baseUrl = 'https://api.binance.com';

// 1. Create a function to sign the request with HMAC SHA256
function sign($query, $secret) {
    return hash_hmac('sha256', $query, $secret);
}

// 2. Create the query string with the required parameters (e.g., timestamp)
$timestamp = round(microtime(true) * 1000);
$queryString = "timestamp=$timestamp";

// 3. Generate the signature using your secret key
$signature = sign($queryString, $apiSecret);

// 4. Final URL for the request with query parameters and the signature
$url = "$baseUrl/api/v3/account?$queryString&signature=$signature";

// 5. Initialize cURL
$ch = curl_init();

// 6. Set cURL options for the request
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    "X-MBX-APIKEY: $apiKey"
]);

// 7. Execute the request and get the response
$response = curl_exec($ch);

// 8. Close the cURL session
curl_close($ch);

// 9. Display the account balance information
if ($response !== false) {
    $responseData = json_decode($response, true);
    if (isset($responseData['balances'])) {
        echo "Account Balances:\n";
        foreach ($responseData['balances'] as $balance) {
            if ($balance['free'] > 0 || $balance['locked'] > 0) {
                echo $balance['asset'] . ": Free = " . $balance['free'] . ", Locked = " . $balance['locked'] . "\n";
            }
        }
    } else {
        echo "Error: " . $responseData['msg'] . "\n";
    }
} else {
    echo "Error fetching the account balance.\n";
}
?>
