- Send WhatsApp messages via PHP using WhatsAPI
- Send WhatsApp messages via PHP using WhatsAPI
- Updates
- 1. Get your WhatsApp password
- 2. Get WhatsAPI and send/receive messages
- 2.1. Download WhatsAPI and test scripts
- 2.2. Send WhatsApp messages
- 2.3. Receive WhatsApp messages
- How to Send a Message by WhatsApp API using PHP easily
- Example usage for WhatsApp API using PHP:
- Send Image
- Send Document
- Send Audio
- Send Voice
- Send Video
- Send Link
- Send Contact
- Send Location
- Send Vcard
- WhatsApp API
- Recent Posts
- send whatsapp message with php (API)
- Template Message
- List Message
- Location
- Conclusion
My name is Philipp C. Heckel and I write about nerdy things.
Send WhatsApp messages via PHP using WhatsAPI
- Jul 07 / 2013
- 160
- Linux, Mobile, PHP, Scripts, WhatsApp
Send WhatsApp messages via PHP using WhatsAPI
I recently discovered that once you have acquired your WhatsApp account password, it’s relatively easy to send and receive WhatsApp messages via PHP. Using the PHP-based framework WhatsAPI, a simple WhatsApp notifier script only has a dozen lines of code.
This tiny tutorial shows how to use the two very basic functions of WhatsAPI, namely to send simple outgoing messages to any number and to listen for new incoming messages from your own WhatsApp account. This is the second part of a two-part tutorial. The first part demonstrated how to sniff the WhatsApp password from your Android phone or iPhone.
Updates
- December 2013: I kindly ask you to stop e-mailing me about hacking into WhatsApp accounts or sniffing WhatsApp passwords for you. Also, I will not help you to send mass WhatsApp messages – even for money. Thank you!
- February 2014: WhatsAPI is down (DCMA infrigement), so using it would be highly questionable — if not illegal. Whether it still works or not: I don’t know. Also: I do not know where to find the latest code.
1. Get your WhatsApp password
This little demonstration only works if you have already obtained your WhatsApp password. If you have not and have no idea how to do it, please check out the first part of this tutorial.
2. Get WhatsAPI and send/receive messages
Assuming you have your WhatsApp password at hand, let’s see how easy the usage of WhatsAPI is.
2.1. Download WhatsAPI and test scripts
Downloading WhatsAPI is really simply since it is hosted on Github. Simply make a new directory and retrieve WhatsAPI from Github.
Once you have done that, you can check out the current structure of the project. There is also a file called EXAMPLES.php (was at https://github.com/venomous0x/WhatsAPI/blob/master/src/php/EXAMPLES.php, site now defunct, July 2019) that shows a few more examples.
I also prepared a few small scripts that you can use as a basis to make your own scripts:
- whatsapp_whatsapi_send.php is a command line script to send any strings to a given number.
- whatsapp_whatsapi_listen.php listens for incoming messages and outputs them to STDOUT.
- whatsapp_whatsapi_config.php defines the configuration (source/destination numbers and WhatsApp password) for the send/listen scripts
- whatsapp_whatsapi_example_messages.txt shows the structure of a few WhatsApp messages (print_r($msgs) output).
To download my two minimal examples, run the following commands, and edit the file whatsapp_whatsapi_config.php to set your own user credentials:
wget — O whatsapp_whatsapi_send .php http : / / blog .philippheckel .com / uploads / 2013 / 07 / whatsapp_whatsapi_send .php .txt
wget — O whatsapp_whatsapi_listen .php http : / / blog .philippheckel .com / uploads / 2013 / 07 / whatsapp_whatsapi_listen .php .txt
wget — O whatsapp_whatsapi_config .php http : / / blog .philippheckel .com / uploads / 2013 / 07 / whatsapp_whatsapi_config .php .txt
wget — O whatsapp_whatsapi_example_messages .txt http : / / blog .philippheckel .com / uploads / 2013 / 07 / whatsapp_whatsapi_example_messages .txt
2.2. Send WhatsApp messages
As you might know from your smartphone client, you can send different kind of messages through WhatsApp: Besides text, you can send audio and video files, locations and contacts. WhatsAPI can do all of those things in just one line of code.
My simple sample script whatsapp_whatsapi_send.php just shows how to send a regular text message. The script is meant to be called by the command line, but the code can also be used in a web application:
The script includes the configuration for your WhatsApp username, password and display name. It’s very easy to use and quite self-explanatory: The WhatsProt class is the only thing you need. Simple Connect to the WhatsApp servers and LoginWithPassword to authenticate yourself. After that, you can use the following methods:
- Message($to, $msg): Simply send a regular text message to $to.
- MessageImage($to, $imageURI): Send images by URL or local path (jpg) to $to.
- MessageVideo($to, $videoURI): Send videos by URL or local path (mp4) to $to.
- MessageAudio($to, $audioURI): Send audios by URL or local path (mp3) to $to.
- Location($to, $lng, $lat): Send GPS coordinates to $to
- vCard($to, $vCardName, $vCard): Send a vCard to $to.
- WaitForReceipt(): Wait for the WhatsApp servers to confirm the delivery.
The tiny script from above obviously only sends plain text messages. You can use it from the command line like this:
The script is particularly useful as a WhatsApp notifier, allowing you to receive notifications from your servers whenever you want — for example, if the CPU temperature rises above a certain threshold, the load is too high for a certain amount of time or one of your scripts failed/succeeded. This is particularly interesting in combination with a system monitoring service such as Nagios or Monit.
2.3. Receive WhatsApp messages
To be able to receive WhatsApp messages using PHP, you need to listen for new messages. WhatsAPI’s PollMessages does exactly that. It reads messages from the WhatsApp server socket and puts them in a local queue for processing. The method blocks if there are no messages and waits for the server to send a message indefinitely — just like any other server does. Using GetMessages you can pull the messages from the queue and process them in your application.
A minimal script would look very similar to the example from above, except that instead of calling Message(), you need to call PollMessages() and GetMessages() in a server loop:
How to Send a Message by WhatsApp API using PHP easily
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);
Send Link
$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 /cdn-cgi/l/email-protection" data-cfemail="efdedbdfdfdfdfdfdfdfdfdeaf8cc19a9c">[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?
WhatsApp API
Check out the link below for more information about WhatsApp API .
Recent Posts
send whatsapp message with php (API)
It’s more interactive than wishing user to respond in a text.
To send this message, you will need to make a multidimensional array, then just encode it to be a json.
Note : max 3 buttons
'https://api.fonnte.com/send', CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => '', CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_POSTFIELDS => array( 'target' => '08123456789', 'url' => 'https://md.fonnte.com/images/logo-dashboard.png', 'buttonJSON' => ',,]>', 'countryCode' => '62', //optional ), CURLOPT_HTTPHEADER => array( 'Authorization: TOKEN' //change TOKEN to your actual token ), )); $response = curl_exec($curl); curl_close($curl); echo $response;
You can add url ofcourse, but it’s limited to image and video only.
Template Message
This feature has been deprecated and no longer maintained
Template button is much more interactive than just button message.
Sadly, our API’s can not send this type to IOS yet.
This message type support call and link.
The other one is just regular button.
Note : max 3 buttons
'https://api.fonnte.com/send', CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => '', CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_POSTFIELDS => array( 'target' => '08123456789', 'templateJSON' => ',,]>', 'countryCode' => '62', //optional ), CURLOPT_HTTPHEADER => array( 'Authorization: TOKEN' //change TOKEN to your actual token ), )); $response = curl_exec($curl); curl_close($curl); echo $response;
Template is not supporting any media yet.
So you can only send plain text with it.
List Message
This feature has been deprecated and no longer maintained
List message is another message type which allow you to create many buttons, not just 3.
And when you click button, this popup appear.
This is great! you can give your user many choices.
And even better, it has group and description for easier understanding of choice.
'https://api.fonnte.com/send', CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => '', CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_POSTFIELDS => array( 'target' => '08123456789', 'listJSON' => ',,]>,,,]>]>', 'countryCode' => '62', //optional ), CURLOPT_HTTPHEADER => array( 'Authorization: TOKEN' //change TOKEN to your actual token ), )); $response = curl_exec($curl); curl_close($curl); echo $response;
With this, you can send list message and waiting for a reply.
Location
Location can be send using latitude and longitude.
the format should be latitude,longitude.
'https://api.fonnte.com/send', CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => '', CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_POSTFIELDS => array( 'target' => '08123456789', 'location' => '-7.983908, 112.621391', ), CURLOPT_HTTPHEADER => array( 'Authorization: TOKEN' //change TOKEN to your actual token ), )); $response = curl_exec($curl); curl_close($curl); echo $response;
You can change the location value with your desired location.
Conclusion
Fonnte provide many configuration to make the API as flexible as possible.
If you are not a programmer or confuse how to do it, you can hire a programmer or just use fonnte’s dashboard to send whatsapp message.
We have cover all this way to send message in our dashboard.
In the end, it’s not only meant for sending, you can also reply the message.
We’ll cover in autoreply tutorial on how to create a whatsapp chat bot.