<?php
// Enable error reporting to help you see if anything goes wrong
ini_set('display_errors', 1);
error_reporting(E_ALL);

// ==========================================
// CONFIGURATION
// ==========================================
// Replace this with your NEW generated key
$apiKey = '8d18baa9b108227abd7800cb216b4be64383c52632b0b3097582b60d69356fd7'; 
$senderId = 'DentalDrip';

/**
 * Sends SMS using URL/Form-Encoded format
 */
function sendSMS($to, $message, $sender, $apiKey) {
    $url = 'https://api.smsonlinegh.com/v5/message/sms/send';

    // Per documentation: Build the data array
    $data = [
        'key'    => $apiKey,      // Key is required in the payload for this API type
        'text'   => $message,
        'to'     => $to,
        'sender' => $sender,
        'type'   => 0             // 0 for text
    ];

    // Convert array to URL-encoded string (e.g., key=val&text=val...)
    $postFields = http_build_query($data);

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
    
    // Set headers for standard form data
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        'Content-Type: application/x-www-form-urlencoded',
        'Accept: application/json'
    ]);

    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    $curlError = curl_error($ch);
    
    curl_close($ch);

    if ($curlError) {
        return ['status' => 0, 'response' => $curlError];
    }

    return ['status' => $httpCode, 'response' => json_decode($response, true)];
}

// ==========================================
// FORM HANDLING
// ==========================================
$statusMessage = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $phone = isset($_POST['phone']) ? trim($_POST['phone']) : '';
    $msg = isset($_POST['message']) ? trim($_POST['message']) : '';
    
    if (empty($phone) || empty($msg)) {
        $statusMessage = "<div style='color:red;'>Please enter both a phone number and a message.</div>";
    } else {
        $result = sendSMS($phone, $msg, $senderId, $apiKey);
        
        // Check for HTTP 200 (Success)
        if ($result['status'] == 200) {
            $statusMessage = "<div style='color:green;'>Success!</div><pre>" . print_r($result['response'], true) . "</pre>";
        } else {
            $statusMessage = "<div style='color:red;'>Error " . $result['status'] . ":</div><pre>" . print_r($result['response'], true) . "</pre>";
        }
    }
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>SMS Test Utility</title>
</head>
<body>
    <h2>Test SMS Sender</h2>
    <form method="POST">
        <label>Recipient (Format: 23324XXXXXXX)</label><br>
        <input type="text" name="phone" required style="width:300px; padding:5px;"><br><br>
        
        <label>Message</label><br>
        <textarea name="message" required style="width:300px; height:100px; padding:5px;"></textarea><br><br>
        
        <button type="submit">Send SMS</button>
    </form>
    <hr>
    <?php echo $statusMessage; ?>
</body>
</html>