'whatsapp', 'to' => $recipient, // E.164 format without the leading + 'type' => 'template', 'template' => [ 'name' => $templateName, 'language' => ['code' => $languageCode], ], ]; $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Authorization: Bearer ' . $token, 'Content-Type: application/json', ]); $response = curl_exec($ch); curl_close($ch); return $response; } // Example usage (uncomment and fill in your credentials): // echo sendWhatsAppTemplateMessage('YOUR_ACCESS_TOKEN', 'YOUR_PHONE_NUMBER_ID', '1234567890', 'hello_world'); // --------------------------------------------------------------------------- // 2) WEBHOOK RECEIVER (handles verification + incoming messages) // --------------------------------------------------------------------------- // Point your Meta App's webhook URL at this script (or a route that calls // handleWhatsAppWebhook()) to receive messages and verification challenges. function handleWhatsAppWebhook(string $verifyToken): void { // Webhook verification (GET request from Meta) if ($_SERVER['REQUEST_METHOD'] === 'GET' && isset($_GET['hub_mode']) && $_GET['hub_mode'] === 'subscribe') { if ($_GET['hub_verify_token'] === $verifyToken) { echo $_GET['hub_challenge']; http_response_code(200); exit; } http_response_code(403); exit; } // Incoming message (POST request) $input = file_get_contents('php://input'); $data = json_decode($input, true); if (isset($data['entry'][0]['changes'][0]['value']['messages'])) { $message = $data['entry'][0]['changes'][0]['value']['messages'][0]; $from = $message['from']; $text = $message['text']['body'] ?? ''; // Replace with your own storage/processing logic (database, queue, CRM, etc.) file_put_contents('whatsapp_log.txt', "Received from $from: $text\n", FILE_APPEND); } http_response_code(200); } // Uncomment to activate this script as your webhook endpoint: // handleWhatsAppWebhook('YOUR_VERIFY_TOKEN');