
<?php

// WhatsApp API credentials
$apiUrl = "https://graph.facebook.com/v20.0/398244606712673/messages";
$accessToken = "EAAjKJ4QeIZBoBOwCf6zKO5EzJTYW79cT6aozFrBPaELvUL3ZBZCcTy0V7ZCcllPmVeSZAoElk1kMZB3iJZB8Ngp5Hal2edIhXoOtuCcPcDUwFLqrIzR5wE23jm0rPbRby5HtbKu1DAjbQ8RFZCSzoCL17ZCo0TxX4ZBfefLBbj9KipJlizrEZBaSxsKXH53dHhKTo19MLFZC9ve59ZAsDZBeibeneAGO3yv5PUC3GEKY0Q76eFVaQdofDvK6SA";

// Recipients' phone numbers
$recipients = [
    "918675776616",
];

// Message data

// List Message
$message = [
        "messaging_product" => "whatsapp",
        "type" => "template",
        "template" => [
        "name" => "welcome",
        "language" => [
            "code" => "en"
        ],
    ]
];


// Function to send a message to a single recipient
function sendMessage($phoneNumber, $message, $apiUrl, $accessToken) {
    $message["to"] = $phoneNumber;

    $ch = curl_init($apiUrl);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        "Authorization: Bearer $accessToken",
        "Content-Type: application/json"
    ]);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($message));

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

    return json_encode($response, true);
}

// Send the message to each recipient
foreach ($recipients as $recipient) {
    $response = sendMessage($recipient, $message, $apiUrl, $accessToken);
    print_r($response);
}

?>
