
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
<?php
declare(strict_types=1);

/**
 * Send SMS using TaQnyat API
 *
 * @param array  $recipients Array of mobile numbers (e.g. ['9665xxxxxxx'])
 * @param string $message    SMS message body
 * @param string $sender     Sender name (approved sender ID)
 * @param string $apiToken   Bearer token from TaQnyat
 *
 * @return array Response array (success / error)
 */
function sendTaqnyatSMS(
    array $recipients,
    string $message,
    string $sender,
    string $apiToken
): array {

    $url = 'https://api.taqnyat.sa/v1/messages';

    $payload = [
        'recipients' => $recipients,
        'body'       => $message,
        'sender'     => $sender,
    ];

    $ch = curl_init($url);

    curl_setopt_array($ch, [
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_POST           => true,
        CURLOPT_HTTPHEADER     => [
            'Authorization: Bearer ' . $apiToken,
            'Content-Type: application/json',
        ],
        CURLOPT_POSTFIELDS     => json_encode($payload, JSON_UNESCAPED_UNICODE),
        CURLOPT_TIMEOUT        => 30,
    ]);

    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    if ($response === false) {
        $error = curl_error($ch);
        curl_close($ch);

        return [
            'success' => false,
            'error'   => $error,
        ];
    }

    curl_close($ch);

    return [
        'success'   => ($httpCode >= 200 && $httpCode < 300),
        'httpCode'  => $httpCode,
        'response'  => json_decode($response, true),
        'raw'       => $response,
    ];
}



$apiToken = 'b0adadc39d8587a8f44058317617bd04';

$result = sendTaqnyatSMS(
    ['966565845759'],
    'Hello From Honda',
    'HONDA',
    $apiToken
);

if ($result['success']) {
    echo 'SMS sent successfully';
} else {
    echo 'SMS failed';
    print_r($result);
}