<?php


$base_url = 'https://api.binance.com';

// Endpoint for server time
$endpoint = '/api/v3/time';

// Initialize cURL session
$ch = curl_init();

// Set cURL options
curl_setopt($ch, CURLOPT_URL, $base_url . $endpoint);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Execute cURL session
$response = curl_exec($ch);

// Check for cURL errors
if ($response === false) {
    echo 'Curl error: ' . curl_error($ch);
    curl_close($ch);
    exit;
}

// Close cURL session
curl_close($ch);

// Decode JSON response
$time_data = json_decode($response, true);

// Print server time
if (isset($time_data['serverTime'])) {
    $server_time = date('Y-m-d H:i:s', $time_data['serverTime'] / 1000); // Convert milliseconds to seconds

    $servertime=$time_data['serverTime'];
    echo 'Server Time: ' . $server_time;
} else {
    echo 'Error: Unable to retrieve server time.';
}

// Binance API credentials
$api_key = 'Xrbk61jyzWkqwt0oIEnJtHRGdCTmkEFlf75WWVZYQyeMFBa98EdqoQ2ibgiEELx3';
$api_secret = '7MMUkUvKzm32E08UCCrkOxeM5FAOVEObi0ckk8CfTfVIEfaug6b8ME6tdlLMPwxl';
$limit='1000';


$timestamp = $servertime;


// Binance API base URL
$base_url = 'https://api.binance.com'; // Note: Futures API uses fapi.binance.com

// Endpoint for fetching all orders
$endpoint = '/api/v3/allOrders';

// Parameters
$symbol = 'SOLUSDT'; // Replace with your trading pair
$limit = 1000; // Max limit is 1000 orders per request

// Generate timestamp


// Build query string
$query_string = http_build_query([
    'symbol' => $symbol,
    'timestamp' => $timestamp,
    'limit' => $limit,
]);

// Create signature
$signature = hash_hmac('sha256', $query_string, $api_secret);

// Build request URL
$url = $base_url . $endpoint . '?' . $query_string . '&signature=' . $signature;

// Initialize cURL session
$ch = curl_init();

// Set cURL options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'X-MBX-APIKEY: ' . $api_key,
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Execute cURL session
$response = curl_exec($ch);

// Check for cURL errors
if ($response === false) {
    echo 'Curl error: ' . curl_error($ch);
    curl_close($ch);
    exit;
}

// Close cURL session
curl_close($ch);

// Decode JSON response
$orders = json_decode($response, true);

// Check if response is successful
if (isset($orders['code'])) {
    echo 'Error: ' . $orders['msg'];
    exit;
}

// Print all orders
echo '<pre>';
print_r($orders);
echo '</pre>';
?>