<?php
global $con;

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

// SMS text
$textBonus = $from . PHP_EOL .
             "Bonus Level " .$level." : RM".$bonus. PHP_EOL .
             "ID: " . $usrnm . PHP_EOL;

// API URL
$url = 'https://sms.360.my/gw/bulk360/v3_0/send.php';

// Prepare data
$data = [
    'user'   => $user,
    'pass'   => $pass,
    'to'     => $to,
    'text'   => $textBonus,
    '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);

// Update DB balance
if ($result && isset($result['balance'])) {
    $balance = round((float)$result['balance'], 2);
    $balance = mysqli_real_escape_string($con, $balance);
    mysqli_query($con, "UPDATE setting SET sms_balance = '$balance'");
}
?>
