Hey there, fellow developer! Ready to supercharge your PHP application with WhatsApp messaging capabilities? You're in the right place. We're going to dive into integrating the WhatsApp API using the awesome eaze/whatsapp-php-sdk
package. This nifty tool will make your life a whole lot easier when it comes to sending and receiving WhatsApp messages programmatically.
Before we jump in, make sure you've got these basics covered:
Let's kick things off by installing our star player, the eaze/whatsapp-php-sdk
. Fire up your terminal and run:
composer require eaze/whatsapp-php-sdk
Easy peasy, right? Composer's got your back.
Now, let's get our ducks in a row with the configuration:
use Eaze\WhatsAppSDK\WhatsAppClient; $client = new WhatsAppClient([ 'api_key' => 'your_api_key_here', 'api_secret' => 'your_api_secret_here', 'phone_number_id' => 'your_phone_number_id' ]);
Replace those placeholder values with your actual credentials, and you're good to go!
Sending a text message is a breeze:
$response = $client->sendTextMessage('recipient_phone_number', 'Hello, WhatsApp!');
Want to spice things up with some media? No problem:
$response = $client->sendImageMessage('recipient_phone_number', 'https://example.com/image.jpg', 'Check out this cool image!');
For those pre-approved templates:
$response = $client->sendTemplateMessage('recipient_phone_number', 'template_name', ['param1', 'param2']);
To handle incoming messages, you'll need to set up a webhook. Create a new PHP file (let's call it webhook.php
) and add:
<?php $input = json_decode(file_get_contents('php://input'), true); $client->handleWebhook($input, function($message) { // Do something with the message echo "Received message: " . $message->getBody(); });
Don't forget to configure this URL in your WhatsApp Business API settings!
Creating a group is straightforward:
$group = $client->createGroup('My Awesome Group', ['1234567890', '0987654321']);
Need to check if a number is registered on WhatsApp?
$isRegistered = $client->checkContact('1234567890');
Always wrap your API calls in try-catch blocks:
try { $response = $client->sendTextMessage('recipient_phone_number', 'Hello, WhatsApp!'); } catch (\Exception $e) { error_log('WhatsApp API Error: ' . $e->getMessage()); }
And there you have it! You're now equipped to integrate WhatsApp messaging into your PHP applications like a pro. Remember, the eaze/whatsapp-php-sdk
documentation is your friend for more advanced usage and features.
Now go forth and build something awesome! Happy coding! 🚀