<?php
// ===== API TOKEN =====
$API_TOKEN = "apit_0rFs3BXZJuw9KfZdKAeQUOARnwfcLCZB-UuRNT";
$SENDER_ID =MOCEAN;

// ===== DATA FORM =====
$to  = preg_replace('/[^0-9]/', '', $_POST['to']);
$msg = trim($_POST['msg']);

if(!$to || !$msg) die("Nomor & pesan wajib diisi");

if(substr($to,0,2)!="62"){
    $to = "62".$to;
}

// ===== KIRIM SMS =====
$url = "https://rest.moceanapi.com/rest/2/sms";

$data = [
    "mocean-token" => $API_TOKEN,
    "mocean-to"    => $to,
    "mocean-from" => $SENDER_ID,
    "mocean-text" => $msg
];

$ch = curl_init($url);
curl_setopt_array($ch,[
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => http_build_query($data),
    CURLOPT_RETURNTRANSFER => true
]);

$response = curl_exec($ch);
curl_close($ch);

$res = json_decode($response,true);

if(isset($res['messages'][0]['status']) && $res['messages'][0]['status']=="0"){
    echo "✅ SMS BERHASIL DIKIRIM";
} else {
    echo "❌ GAGAL KIRIM<br><pre>$response</pre>";
}
?>