How can i send and receive messages on whatsapp using php?

Do I need to convert/register my personal number to business account?

Not necessarily, it's your choice if you want to share your personal account, even though you recommend using a business account

Is there any API provided by whatsapp for this?

According to Whatsapp's own documentation, they do not have a direct API, but you can create a link to send the messages, according to the documentation, follow the submission parameter by the URL: https://api.whatsapp.com/send?phone=

Here's another example:

Use: https://api.whatsapp.com/send?phone=15551234567

Don't use: https://api.whatsapp.com/send?phone=+001-(555)1234567

You can see more in: https://faq.whatsapp.com/en/android/26000030/


UPDATE APRIL, 24 2022

Whatsapp sender API url has been changed to: https://wa.me/<number>

Example:

Use: https://wa.me/1XXXXXXXXXX

Don't use: https://wa.me/+001-(XXX)XXXXXXX

How can i send and receive messages on whatsapp using php?

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.


Contents

  1. Updates
  2. 1. Get your WhatsApp password
  3. 2. Get WhatsAPI and send/receive messages
    1. 2.1. Download WhatsAPI and test scripts
    2. 2.2. Send WhatsApp messages
    3. 2.3. Receive WhatsApp messages


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.

mkdirwhatsapp

cdwhatsapp

sudo apt-getinstall git

git clonehttps://github.com/venomous0x/WhatsAPI

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-Owhatsapp_whatsapi_send.phphttp://blog.philippheckel.com/uploads/2013/07/whatsapp_whatsapi_send.php.txt

wget -Owhatsapp_whatsapi_listen.phphttp://blog.philippheckel.com/uploads/2013/07/whatsapp_whatsapi_listen.php.txt

wget-O whatsapp_whatsapi_config.phphttp://blog.philippheckel.com/uploads/2013/07/whatsapp_whatsapi_config.php.txt

wget-O whatsapp_whatsapi_example_messages.txthttp://blog.philippheckel.com/uploads/2013/07/whatsapp_whatsapi_example_messages.txt

chmod+x*.php

vi whatsapp_whatsapi_config.php

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:

#!/usr/bin/php

<!--?php

require_once('whatsapp_whatsapi_config.php');

$destinationPhone='495553333333';

$w=new WhatsProt($userPhone,$userIdentity,$userName,$debug);

$w--->Connect();

$w-&gt;LoginWithPassword($password);

$w-&gt;Message($destinationPhone,$argv[1]);

?&gt;

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:

./whatsapp_whatsapi_send.php"Warning: CPU temperature at 65°C"

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:

<!--?php

require_once('whatsapp_whatsapi_config.php');

$w=newWhatsProt($userPhone,$userIdentity,$userName, $debug);

$w--->Connect();

$w-&gt;LoginWithPassword($password);

while(true){

    $w-&gt;PollMessages();

    $msgs=$w-&gt;GetMessages();

    // Do something with the messages ...

}

?&gt;

Each WhatsApp message has a set of standard attributes ($m->_attributeHash) such as from (sender number) or t (send timestamp). Additionally, it has different kind of child nodes that contain additional/optional information, depending on what type of message it is: a notify child node, for instance, tells the conversation partner that he or she is online and still writing, and the body child node contains the text contents. There are many more of these. You can see for yourself by calling print_r($msgs).

The following snippet shows an excerpt of one message — refer to this example output to see more:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

ProtocolNode Object

(

    [_tag]=&gt;message

    [_attributeHash]=&gt;Array

        (

            [from]=&gt;491231234567@s.whatsapp.net

            [id]=&gt; 1373204559-6

            [type]=&gt;chat

            [t]=&gt; 1373205620

        )

    [_children]=&gt;Array

        (

         ...

            [2] =&gt;ProtocolNode Object

                (

                    [_tag]=&gt; body

                    ...

                    [_children]=&gt;

                    [_data] =&gt;Hallo blog readers

                )

        )

    [_data]=&gt;

)

My example server script whatsapp_whatsapi_listen.php extends the above snippet and processes the messages like this: It takes the time (t) and sender number (from) from $m->_attributeHash and the name and _data from the child nodes. Each non-empty message is printed to STDOUT, like this:

./whatsapp_whatsapi_listen.php

[07/07/201315:57]From:491231234567,Name:Philipp, Message:Hallo blog readers

[07/07/201315:57]From:491231234567,Name:Philipp, Message:EverythingIwriteisprinted toSTDOUT

[07/07/201315:57]From: 491231234567,Name:Philipp,Message:Exit

If the message body is “exit”, the script exits.

That’s it. I hope this tutorial helped a little in understanding how WhatsAPI works. If you have any suggestions or questions, please let me know in the comments.

I'd very much like to hear what you think of this post. Feel free to leave a comment. I usually respond within a day or two, sometimes even faster. I will not share or publish your e-mail address anywhere.

How can I use WhatsApp API to send a message in php?

Here's another example:.
Use: https://api.whatsapp.com/send? phone=15551234567..
Don't use: https://api.whatsapp.com/send? phone=+001-(555)1234567..
UPDATE APRIL, 24 2022..
Use: https://wa.me/1XXXXXXXXXX..
Don't use: https://wa.me/+001-(XXX)XXXXXXX..

How can I send and receive messages on WhatsApp on PC?

Go to https://www.whatsapp.com/ in a web browser. As long as you have a WhatsApp account, you can use the desktop app to send messages from your computer.

How can I use WhatsApp API for free?

How to get a free account. You can register your account via email, or you can sign in directly to app.chat-api.com using your Google Account or GitHub Account. You need to create a developer instance on the "Add new instance" page. A developer instance can only be a single instance on an account.

Can I send WhatsApp message from website?

Open the web browser and then paste 'https://api.WhatsApp.com/send?phone=number' in the Address bar of your phone's browser. In the place of “number”, enter the phone number of the person to whom you want to send a WhatsApp message with the country code.