1. Go to this page and download the library: Download maina-david/whatsapp-sdk library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
use MainaDavid\WhatsAppSDK\WhatsApp;
public function sendMessage()
{
try {
// Instantiate the Whatsapp class with the necessary parameters
$whatsapp = new WhatsApp($phoneNumberId, $accessToken);
// Prepare the options for sending a text message
$options = [
'to' => '+123456789', // Replace with the recipient's phone number
'message' => 'Hello, world!' // Replace with the message content
];
// Call the sendTextMessage() method of the Whatsapp class to send the message
$response = $whatsapp->sendTextMessage($options);
// Handle the response or perform other actions
if ($response['status'] === 'success') {
// Message sent successfully
return response()->json(['message' => 'Message sent']);
} else {
// Failed to send message
return response()->json(['message' => 'Failed to send message']);
}
} catch (Exception $e) {
// Handle any exceptions that occur during initialization or API calls
return response()->json(['message' => $e->getMessage()], 500);
}
}