<?php
function sendWhatsAppMessage($to, $message) {
    // Your Meta WhatsApp Cloud API credentials
    $token = "YOUR_PERMANENT_ACCESS_TOKEN"; // from Meta
    $phone_number_id = "YOUR_PHONE_NUMBER_ID"; // from Meta
    
    $url = "https://graph.facebook.com/v17.0/$phone_number_id/messages";

    $data = [
        "messaging_product" => "whatsapp",
        "to" => $to,  // recipient number (with country code, e.g., 919812345678)
        "type" => "text",
        "text" => ["body" => $message]
    ];

    $options = [
        "http" => [
            "header"  => "Authorization: Bearer $token\r\n" .
                         "Content-Type: application/json\r\n",
            "method"  => "POST",
            "content" => json_encode($data),
        ],
    ];

    $context  = stream_context_create($options);
    $result = file_get_contents($url, false, $context);

    if ($result === FALSE) {
        return ["success" => false, "response" => "Error sending message"];
    } else {
        return ["success" => true, "response" => $result];
    }
}

// Example usage:
$response = sendWhatsAppMessage("919812345678", "Hello! This is a WhatsApp message from PHP function.");
print_r($response);
