<?php

// Your OKX API credentials
$api_key = "3ba4dd16-78d0-45ef-9d5d-4095abd68355";
$secret_key = "91B339B9A70C73E832ED445C2842A9BF";
$passphrase = "S!valeeskp9969";

$base_url = "https://www.okx.com";
$endpoint = "/api/v5/account/balance";

// Generate timestamp (OKX requires UTC timestamp)
$timestamp = gmdate("Y-m-d\TH:i:s\.000\Z");

// Function to generate OKX API signature
function generateSignature($timestamp, $method, $endpoint, $body, $secret_key) {
    $message = $timestamp . $method . $endpoint . $body;
    return base64_encode(hash_hmac('sha256', $message, $secret_key, true));
}

// Create signature
$signature = generateSignature($timestamp, "GET", $endpoint, "", $secret_key);

// Set cURL options
$curl = curl_init();
curl_setopt_array($curl, [
    CURLOPT_URL => $base_url . $endpoint,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => [
        "OK-ACCESS-KEY: $api_key",
        "OK-ACCESS-SIGN: $signature",
        "OK-ACCESS-TIMESTAMP: $timestamp",
        "OK-ACCESS-PASSPHRASE: $passphrase",
        "Content-Type: application/json"
    ],
]);

// Execute cURL request
$response = curl_exec($curl);
echo '<pre>';
print_r($response);
echo '</pre>';
$http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);

// Output response
if ($http_code == 200) {
    echo "Balance Info: " . $response;
} else {
    echo "Error fetching balance. HTTP Code: " . $http_code;
}
?>