<?php 
include('config.php');
$api_key=$_POST['api_key'];
$api_secret=$_POST['api_secret'];
$userid=$_SESSION['userid'];
$flag=0;
$api_key = $api_key;
$secret_key = $api_secret;
$base_url = "https://api.binance.com";
$endpoint = "/api/v3/account";

//$timestamp = round(microtime(true) * 1000); // Get current time in milliseconds

$timestamp=get_time();
$query_string = "timestamp=$timestamp";
$signature = hash_hmac('sha256', $query_string, $secret_key);

$url = "$base_url$endpoint?$query_string&signature=$signature";

$headers = [
    "X-MBX-APIKEY: $api_key",
    "Content-Type: application/json"
];

$curl = curl_init();
curl_setopt_array($curl, [
    CURLOPT_URL => $url,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => $headers,
]);

$response = curl_exec($curl);
$http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);

// Output response
if ($http_code == 200) 
{
    $res=mysqli_num_rows(mysqli_query($conn,"select * from mul_ex_apis where type='1' and userid='".$userid."'"));
    if($res > 0)
    {
        $result= mysqli_query($conn,"select * from mul_ex_apis where type='1' and userid='".$userid."'");
        $result=mysqli_fetch_array($result);
        mysqli_query($conn, "update from mul_ex_apis set api_key='".$api_key."', api_secret='".$api_secret."' where id='".$result['Id']."'");
        
    }
    else
    {
        //echo "insert into mul_ex_apis set api_key='".$api_key."', api_secret='".$api_secret."', userid='".$userid."', type='1'";
        mysqli_query($conn,"insert into mul_ex_apis set api_key='".$api_key."', api_secret='".$api_secret."', userid='".$userid."', type='1'");
    }
    $_SESSION['success']="Binance API Successfully Updated...";
    echo "success";     
} 
else 
{
    echo "Response: " . $response;
}

?>