How to Send a Message by WhatsApp API using PHP
In this tutorial, we will learn How to Send a Message by WhatsApp API using PHP. We will create a simple page to send messages via WhatsApp API using PHP. I will explain How to Send a Message by WhatsApp API using PHP. We are using a Lightweight PHP library for WhatsApp API to send WhatsApp messages in PHP provided by ultramsg.com.
We are use ultramsg/whatsapp-php-sdk
composer plugin. So don’t waste time to start sending a Message by WhatsApp API using PHP.
How to Send a Message by WhatsApp API using PHP
Table of Contents
- Install whatsapp-php-sdk
- Authentication
- Send Hello word Example
- Send Image Example
- Send Audio Example
- Send Document Example
- Send Link Example
- Send Location Example
- Send Video Example
1. Install whatsapp-php-sdk
Just run the below command:
composer require ultramsg/whatsapp-php-sdk
2. Authentication
Now you can Sign up and Go to your instance dashboard. then after you copy the Instance ID and Token which will be used for authenticating.
3. Send Hello word Example
Send Hello word WhatsApp message.
<?php
#index.php
require_once ('vendor/autoload.php');
$token= = "*****"; // Ultramsg token
$instance_id = "*****"; // Ultramsg instance id
$client = new UltraMsg\WhatsAppApi($token, $instance_id);
$to = "+919737058839"; // Your Mobile Number
$body = "Hello world"; // Message
$response = $client->sendChatMessage($to, $body);
print_r($response);
Here, $to: The recipient’s WhatsApp number with an international format like., +919737058839 or chatID or group e.g 9737058839@c.us and $body: in what you send Message.
4. Send Image Example
Note: Supported extensions (jpg, jpeg , gif, png, svg , webp , bmp)
<?php
#image.php
$to = "+919737058839"; // Your Mobile Number
$caption = "Devnote Logo"; // Image caption
$image = "https://devnote.in/wp-content/uploads/2021/11/devnote.png"; // Image URL
$response = $client->sendImageMessage($to, $image, $caption);
print_r($response);
5. Send Audio Example
Note: Supported extensions (mp3, aac, ogg)
<?php
#audio.php
$to = "+919737058839"; // Your Mobile Number
$audio = "https://file-examples.com/storage/fefe3c760763a87999556e8/2017/11/file_example_MP3_700KB.mp3"; \\ Audio URL
$response = $client->sendAudioMessage($to, $audio);
print_r($response);
6. Send Document Example
Note: Supported most extensions like (zip, xlsx, csv, txt, pptx, docx, …etc)
<?php
#document.php
$to = "+919737058839"; // Your Mobile Number
$caption="Dummy PDF"; \\ Document Caption
$document="https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf"; \\ Document URL
$response = $client->sendDocumentMessage($to, $caption, $document);
print_r($response);
7. Send Link Example
<?php
#link.php
$to = "+919737058839"; // Your Mobile Number
$link = "https://devnote.in";
$response = $client->sendLinkMessage($to, $link);
print_r($response);
8. Send Location Example
<?php
#location.php
$to = "+919737058839"; // Your Mobile Number
$address = "Devnote company \n First floor, office no. 101 \n Morbi - 363641";
$latitude = "22.803711809090174";
$longitude = "70.81826296866811";
$response = $client->sendLocationMessage($to, $address, $latitude, $longitude);
print_r($response);
9. Send Video Example
Note: Supported extensions (mp4, 3gp, mov)
<?php
#video.php
$to = "+919737058839"; // Your Mobile Number
$video = "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ForBiggerFun.mp4";
$response = $client->sendVideoMessage($to, $video);
print_r($response);
More Information: https://github.com/ultramsg/whatsapp-php-sdk