Whatsapp API send message PHP

How to Send a Message by WhatsApp API using PHP

Before you begin, ensure that you have installed PHP and the curl extension on your server or local machine.

Authentication

Sign up and connect any phone number to the channel that appears on the dashboard.

Don't worry, you can disconnect at any time, but it's necessary for WhatsApp automation. To do this, navigate to your personal account where you'll find a QR code for the available channel. Then, open WhatsApp on your mobile device, go to Settings -> Linked devices -> Link a device -> Scan QR code.

Now save the generated API-Key token:

How to Send WhatsApp API Message using PHP

Send your first WhatsApp message 😊

To send a text message, you need to make a request to:

POST https://gate.whapi.cloud/messages/text

const BASE_URL = 'https://gate.whapi.cloud/';
$token = 'bg3FeZJ6jWG2PRnw32g0oNkKO7k03GtX'; // Specify your channel token here
$data = array (
    'to' => '14409416972@s.whatsapp.net',
    'body' => 'Hello, world!',
    'typing_time' => 0,
    'view_once' => true,
);
$data = json_encode($data);
$url = BASE_URL . 'messages/text';
$options = array('http' => array(
    'method'  => 'POST',
    'header' => 'Authorization: Bearer '.$token,
    'content' =>  json_encode($data)
));
$context  = stream_context_create($options);
$response = file_get_contents($url, false, $context);
  • 'to': The recipient’s WhatsApp number with international format e.g., 14409416972 or chatID for contact or group e.g 14409416972@s.whatsapp.net or 120361234567890@g.us

  • 'body' : Message text.

Save the PHP script and run it on your server. The specified message will be sent to the specified WhatsApp number. Remember to replace variables with your actual details.

Please note that this is a basic example and doesn't include any error handling. For a production environment, ensure to add appropriate error and exception handling. Please don't hesitate to reach out to share your experiences or pose any questions you might have.

Quickstart

Use the source code of ready-made WhatsApp bots as a platform for an easy start. Download, insert your token and launch!

Get Started Free


Send Media Message

Additional endpoint for easy send media-file as message. Use request body as file and inpath parameters for send parameters. Media message can be one of the following types:

Send Image

Send Document

Send Audio

Send Voice

You can Send a ppt audio recording to the phone number or group, But WhatsApp is sensitive to this extension, you need to be in OGG format and the codecs should be the opus, For more information, you can check out this article.

Send Video

Send GIF

Send Contact

Send Contact List

Send Location

Send Sticker

Last updated