function SendMessagefirst_homework($contactNumber, $messageContent, $watsapInstance, $username, $apiKey,$imgUrl, $ttt) { $apiKey='supersecret123'; // ๐Ÿงน Clean contact number $NewContact = preg_replace('/[\s-]+/', '', $contactNumber); if (strpos($NewContact, '+60') === 0) { $cleanContact = ltrim($NewContact, '+'); $NewContact = $cleanContact . '@c.us'; } else { $cleanContact = ltrim($NewContact, '+'); $cleanContact = ltrim($cleanContact, '0'); if (strlen($cleanContact) >= 12) { $NewContact = substr($cleanContact, -12); } else { $NewContact = '92' . substr($cleanContact, -10); } $NewContact .= '@c.us'; } // ๐Ÿ“ท Image URL $schoolLogo =$imgUrl;// "https://stasuelectric.com/SchoolEventSystem/Student-Attendance-System01-main/Admin/img/" . $username . ".jpg"; // WAHA API endpoint $sendURL = rtrim($watsapInstance, '/') . "/api/sendImage"; // JSON body $data = [ "session" => $username, "chatId" => $NewContact, "file" => [ "mimetype" => "image/jpeg", "url" => $schoolLogo, "filename" => $ttt ], "caption" => $messageContent ]; // HTTP headers with Bearer token $options = [ 'http' => [ 'header' => "Content-Type: application/json\r\n" . "Accept: application/json\r\n" . "X-Api-Key: $apiKey\r\n", // โœ… Correct header 'method' => 'POST', 'content' => json_encode($data), 'ignore_errors' => true ], ]; $context = stream_context_create($options); $result = @file_get_contents($sendURL, false, $context); // ๐Ÿ“ Log everything for debugging // file_put_contents("my.log", "\n\n--- SEND MESSAGE ---\n", FILE_APPEND); /// file_put_contents("my.log", "TIME: " . date('Y-m-d H:i:s') . "\n", FILE_APPEND); // file_put_contents("my.log", "URL: $sendURL\n", FILE_APPEND); // file_put_contents("my.log", "CONTACT: $NewContact\n", FILE_APPEND); // file_put_contents("my.log", "SESSION: $username\n", FILE_APPEND); // file_put_contents("my.log", "MESSAGE: $messageContent\n", FILE_APPEND); // file_put_contents("my.log", "REQUEST BODY: " . json_encode($data) . "\n", FILE_APPEND); /// file_put_contents("my.log", "RESPONSE: " . htmlspecialchars($result) . "\n", FILE_APPEND); if ($result === false) { file_put_contents("my.log", "ERROR: " . print_r(error_get_last(), true) . "\n", FILE_APPEND); } }