This tutorial shows how to send WhatsApp messages using the Twilio PHP SDK by prefixing both the sender and recipient numbers with the whatsapp: scheme inside the messages create call. It covers the Twilio WhatsApp sandbox for development and the steps required to move to a production WhatsApp Business Account. You will send your first WhatsApp message from PHP and verify it arrives in the WhatsApp app on your test device.
Prerequisites and Setup
Install the Twilio SDK with composer require twilio/sdk vlucas/phpdotenv and configure your credentials in a .env file. Join the Twilio WhatsApp sandbox by sending the sandbox join keyword from your WhatsApp account to the sandbox number shown in the Twilio Console under Messaging, Try it out, Send a WhatsApp message. The sandbox sender number is always +14155238886 and your from option must be prefixed as whatsapp:+14155238886 in your PHP code. Sandbox outbound messages must match one of Twilio's pre-approved templates unless the recipient has messaged you first within the active 24-hour conversation window.
composer require twilio/sdk vlucas/phpdotenv
# .env
TWILIO_ACCOUNT_SID=ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
TWILIO_AUTH_TOKEN=your_auth_token_here
TWILIO_WHATSAPP_NUMBER=whatsapp:+14155238886
The Complete Code
The only difference between sending a WhatsApp message and an SMS in the Twilio PHP SDK is the whatsapp: prefix on both the from option value and the $to argument. The message body, mediaUrl, and statusCallback options all work identically to standard SMS. The returned message object has the same sid and status properties, making WhatsApp messages trackable through the same delivery pipeline you use for SMS. WhatsApp-specific error codes start in the 63xxx range and are returned as the status code on a caught RestException.
<?php
require_once 'vendor/autoload.php';
use TwilioRestClient;
use TwilioExceptionsRestException;
$dotenv = DotenvDotenv::createImmutable(__DIR__);
$dotenv->load();
$client = new Client($_ENV['TWILIO_ACCOUNT_SID'], $_ENV['TWILIO_AUTH_TOKEN']);
try {
$message = $client->messages->create(
'whatsapp:+15551234567',
[
'from' => $_ENV['TWILIO_WHATSAPP_NUMBER'],
'body' => 'Hello from Telphi Consulting via WhatsApp!',
]
);
echo 'WhatsApp SID: ' . $message->sid . PHP_EOL;
echo 'Status: ' . $message->status . PHP_EOL;
} catch (RestException $e) {
echo 'Error ' . $e->getStatusCode() . ': ' . $e->getMessage() . PHP_EOL;
}
Testing and Debugging
Run the script and open your WhatsApp app to confirm the message arrives from the Twilio sandbox number. If you receive a RestException with status code 63016, your number has not joined the sandbox; send the join keyword from your WhatsApp account to the sandbox number and retry. Status code 63007 means the recipient number is not registered on WhatsApp; verify the number exists on a real device before testing. Check the Twilio Console under Messaging Logs for the full delivery status chain if the script reports success but the WhatsApp message does not appear in your app.
Taking It Further
Apply for a WhatsApp Business Account through Twilio to move out of the sandbox and send messages to any WhatsApp user without requiring them to opt in to a sandbox first. Use Twilio's Content Template Builder to create pre-approved message templates for transactional notifications such as order confirmations, shipping updates, and appointment reminders. Add 'mediaUrl' => ['https://example.com/invoice.pdf'] to the options array to send documents, images, or audio clips as WhatsApp attachments alongside the message text. Build a bidirectional WhatsApp bot by combining this outbound code with an inbound PHP webhook that parses $_POST['Body'] and replies with MessagingResponse.
Contact Telphi Consulting to configure a WhatsApp Business channel with approved templates and automated conversation handling in PHP.
Ready to Transform Your Business Communications?
Get a free consultation with our VoIP experts and discover how we can help you save costs, improve efficiency, and scale your business.
Comments (0)
Join the discussion and share your thoughts (AI-moderated for quality)
Be the first to comment
No comments yet. Share your thoughts below.