Help Desk
Search
K
Comment on page

Whatsapp API send message PHP

How to Send a Message by WhatsApp API using PHP

Introduction

Whapi.Cloud provides WhatsApp API that allows companies to integrate or automate their work through WhatsApp. In this article, we will show you how to perform the simplest and most necessary action: send a message using the WhatsApp API with 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.
Pair any number
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:
Grab your token

How to Send WhatsApp API Message using PHP

Send your first WhatsApp message 😊
We offer a convenient service for testing requests and using ready-made code fragments in any programming language. You will only need to transfer the saved API-token and fill in the required parameters:
Paste your API token and parameters, then copy code snippet
const BASE_URL = 'https://gate.whapi.cloud/';
$token = 'bg3FeZJ6jWG2PRnw32g0oNkKO7k03GtX'; // Specify your channel token here
$data = array (
'to' => '[email protected]',
'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 [email protected] or [email protected]
  • '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.
Detailed instructions on how to create a bot in PHP: https://whapi.cloud/whatsapp-bot-php

Quickstart

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

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