How to Send a Message by WhatsApp API using PHP easily

Introduction

In this tutorial, we will create a page to send message via WhatsApp API using PHP

Installation

Just download ultramsg.class.php or use Composer:

composer require ultramsg/whatsapp-php-sdk

Authentication

Sign up and Go to your instance dashboard and copy your Instance ID and Token which will be used for authenticating.

Example usage for WhatsApp API using PHP:

Send your first WhatsApp message

require_once ('vendor/autoload.php'); // if you use Composer
//require_once('ultramsg.class.php'); // if you download ultramsg.class.php
	
$token="tof7lsdJasdloaa57e"; // Ultramsg.com token
$instance_id="instance1150"; // Ultramsg.com instance id
$client = new UltraMsg\WhatsAppApi($token,$instance_id);
	
$to="put_your_mobile_number_here"; 
$body="Hello world"; 
$api=$client->sendChatMessage($to,$body);
print_r($api);
  • $to: The recipient’s WhatsApp number with international format e.g., +14155552671 or chatID for contact or group e.g [email protected] or [email protected]
  • $body : Message text.

Note: You can see here how to send WhatsApp messages to a group.

Send Image

$to="put_your_mobile_number_here"; 
$caption="image Caption"; 
$image="https://file-example.s3-accelerate.amazonaws.com/images/test.jpg"; 
$api=$client->sendImageMessage($to,$image,$caption);
print_r($api);

Send Document

$to="put_your_mobile_number_here"; 
$filename="image Caption"; 
$document="https://file-example.s3-accelerate.amazonaws.com/documents/cv.pdf"; 
$api=$client->sendDocumentMessage($to,$filename,$document);
print_r($api);

Send Audio

$to="put_your_mobile_number_here"; 
$audio="https://file-example.s3-accelerate.amazonaws.com/audio/2.mp3"; 
$api=$client->sendAudioMessage($to,$audio);
print_r($api);

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.

$to="put_your_mobile_number_here"; 
$audio="https://file-example.s3-accelerate.amazonaws.com/voice/oog_example.ogg"; 
$api=$client->sendVoiceMessage($to,$audio);
print_r($api);

Send Video

$to="put_your_mobile_number_here"; 
$video="https://file-example.s3-accelerate.amazonaws.com/video/test.mp4"; 
$api=$client->sendVideoMessage($to,$video);
print_r($api);
$to="put_your_mobile_number_here"; 
$link="https://ultramsg.com"; $api=$client->sendLinkMessage($to,$link);
print_r($api);

Send Contact

$to="put_your_mobile_number_here"; 
$contact="[email protected]"; 
$api=$client->sendContactMessage($to,$contact);
print_r($api);

Send Location

$to="put_your_mobile_number_here"; 
$address="ABC company \n Sixth floor , office 38"; 
$lat="25.197197"; 
$lng="55.2721877"; 
$api=$client->sendLocationMessage($to,$address,$lat,$lng);
print_r($api);

Send Vcard

$to="put_your_mobile_number_here"; 
$vcard="BEGIN:VCARD
VERSION:3.0
N:lastname;firstname
FN:firstname lastname
TEL;TYPE=CELL;waid=14000000001:14000000002
NICKNAME:nickname
BDAY:01.01.1987
X-GENDER:M
NOTE:note
ADR;TYPE=home
ADR;TYPE=work
END:VCARD";
$vcard = preg_replace("/[\n\r]/", "\n", $vcard);
$api=$client->sendVcardMessage($to,$vcard);
print_r($api)

You can see the previous steps in this video, and you can see Full WhatsApp API Documentation.

Are you using Ultramsg WhatsApp API PHP SDK?