<?
// API credentials
$user = '4w9aAzzbvM';
$pass = 'vHH3NlIMtymQb5MPi0CSUeJxjERdnkNNJkpPu39X';
$to = '6'.$telefon;
$from = 'RM0 TLS2U:';

// URL dan SMS text
$text = $from . PHP_EOL .
        "Congratulations! Your registration was successful." . PHP_EOL .
        "ID: " . $usrnm . PHP_EOL .
        "Pass: " . $pswd;
        
// API URL
$url = 'https://sms.360.my/gw/bulk360/v3_0/send.php';

// Prepare data
$data = [
    'user' => $user,
    'pass' => $pass,
    'to' => $to,
    'text' => $text,
    'detail' => 1
];

// Initialize cURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Execute and get response
$response = curl_exec($ch);
curl_close($ch);

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

// Output response
if ($result) {

    $balance = $result['balance'] ?? 0;
    $balance = round($balance, 2); // keep 2 decimals only
    mysqli_query($con, "UPDATE setting SET sms_balance = '".mysqli_real_escape_string($con, $balance)."'");


   /// Example: $result from API
    $balance = $result['balance'] ?? 0;
    
    // Update DB (safe)
    $balance = mysqli_real_escape_string($con, $balance);
    mysqli_query($con, "UPDATE setting SET sms_balance = '$balance'");
}

?>
