Send messages to Whatsapp number using PHP

Send messages to Whatsapp number using PHP

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Send messages to Whatsapp number using PHP

You cannot send WhatsApp messages programmatically by simply calling a standard HTTP endpoint or using the public WhatsApp Web API directly from your own server IP. The protocol is encrypted end-to-end, and Meta has restricted access to prevent spam and unauthorized broadcasting. To automate messaging in a production environment, you must utilize the WhatsApp Business Platform, formerly known as the WhatsApp Business API.

This platform operates on a request-response model managed by official Business Solution Providers (BSPs). You do not register your personal number for this; instead, you register a business phone number that becomes your "Sender ID." This number must be verified by Meta, which often involves sending an SMS or making a call to the number to confirm ownership. Once verified, this number can send messages to any WhatsApp user, whether they are using a personal account or a business account.

Architectural Requirements

Before writing a single line of PHP code, you need to establish the infrastructure that connects your server to Meta's ecosystem. The most critical component is a Business Solution Provider. While Meta offers the API, they do not host it directly for developers without a partner layer. Common providers include Twilio, MessageBird, Vonage, or Wati.

These providers abstract the complexity of webhooks, token management, and message templates into SDKs or simple REST APIs. You will need to:

  1. Register a Business Number: Use a real phone number (mobile or landline) that you own. Virtual numbers from VoIP services like Twilio often fail verification unless specifically configured for WhatsApp.
  2. Create a Developer Account: Sign up with your chosen BSP to obtain an API key and access credentials.
  3. Configure Webhooks: Your PHP application must listen for incoming messages. The BSP will forward user replies to a URL you define in their dashboard.

Implementation Strategy in PHP

The most common approach for Laravel developers is using the official SDKs provided by BSPs. For example, the Twilio PHP SDK simplifies the interaction significantly compared to raw HTTP requests.

To send a message, your application typically constructs an HTTP POST request containing a JSON payload with the recipient's phone number and the message content. The phone number must be formatted in E.164 international format (e.g., +447700900123).

Here is how you might structure the logic within a Laravel controller or service class:

use Twilio\Rest\Client;

class WhatsAppService
{
    protected $accountSid;
    protected $authToken;

    public function __construct($accountSid, $authToken)
    {
        $this->accountSid = $accountSid;
        $this->authToken = $authToken;
    }

    public function sendText(string $to, string $messageBody): array
    {
        // Initialize the Twilio client with your credentials
        $client = new Client($this->accountSid, $this->authToken);

        try {
            // Use the Messaging service to send the text
            return $client->messages->create(
                'whatsapp:' . $to, 
                [
                    'from' => env('WHATSAPP_PHONE_NUMBER_SID'), // Your verified business number
                    'body' => $messageBody,
                    // Optional: Add a link or media attachment here
                ]
            )->toArray();
        } catch (Exception $e) {
            // Log the error and handle it appropriately
            return ['error' => $e->getMessage()];
        }
    }
}

Critical Constraints and Message Types

A major pitfall for developers is misunderstanding the 24-hour session window. The WhatsApp Business API does not allow you to send "initiated" marketing messages (unsolicited ads) to users who have never interacted with your business. You can only send:

  1. Template Messages: Pre-approved content used to start a conversation with a new user. These must be approved by Meta before use.
  2. Conversation Responses: Messages sent in reply to a user's message within the last 24 hours of interaction.

Once you send a template message or receive a reply from a user, the "session" opens. You have 24 hours to send up to four messages (one text, one image/video/audio, and two interactive buttons) to that specific user. After 24 hours expire without further interaction, the channel closes until the user replies again or you send another approved template message.

Attempting to bypass this restriction by using unofficial libraries or personal accounts will result in your phone number being banned immediately. There is no gray area; Meta's enforcement is strict to prevent spam.

Handling Incoming Messages

Your PHP application must also handle incoming messages to maintain the conversation flow. This requires setting up a webhook endpoint that accepts POST requests from the BSP.

// Example Laravel Route for Webhook
Route::post('/webhook', function (Request $request) {
    $messages = json_decode($request->getContent(), true);

    foreach ($messages as $message) {
        if ($message['event'] === 'messages.create') {
            // Logic to process the incoming text or media
            echo "Received from {$message['from']}: " . $message['message']['text'];
            
            // Trigger a response logic, perhaps via a background queue job
            WhatsAppService::queueReply($message['from'], "Thanks for your message.");
        }
    }
});

It is vital to validate the signature provided by the BSP (like Twilio) on every incoming request to ensure the webhook calls are legitimate and not forged. Ignoring this step leaves your application vulnerable to spoofed messages.

Conclusion

Sending WhatsApp messages via PHP requires leveraging an official Business Solution Provider rather than attempting direct API access. You must register a verified business number, adhere strictly to the template approval process for cold outreach, and respect the 24-hour session windows for active conversations. By integrating these constraints into your application logic using established SDKs, you ensure compliance with Meta's policies and maintain the trust of your users.

Our network