Send WhatsApp messages via PHP using WhatsAPI By :- Your_Sam** Hacking Tips N Tricks.
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.
======================================================================
Script
mkdir whatsapp
cd whatsapp
sudo apt-get install git
git clone https://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 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:
==========================================================================
Script
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
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:
====================================================================
Script
#!/usr/bin/php
<?php
require_once('whatsapp_whatsapi_config.php');
$destinationPhone = '495553333333';
$w = new WhatsProt($userPhone, $userIdentity, $userName, $debug);
$w->Connect();
$w->LoginWithPassword($password);
$w->Message($destinationPhone, $argv[1]);
?>
=======================================================================
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:==
=======================================================================
Script
./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:
=========================================================================
Script
<?php
require_once('whatsapp_whatsapi_config.php');
$w = new WhatsProt($userPhone, $userIdentity, $userName, $debug);
$w->Connect();
$w->LoginWithPassword($password);
while (true) {
$w->PollMessages();
$msgs = $w->GetMessages();
// Do something with the messages ...
}
?>
==========================================================================
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:
========================================================================
Script
ProtocolNode Object
(
[_tag] => message
[_attributeHash] => Array
(
[from] => 491231234567@s.whatsapp.net
[id] => 1373204559-6
[type] => chat
[t] => 1373205620
)
[_children] => Array
(
...
[2] => ProtocolNode Object
(
[_tag] => body
...
[_children] =>
[_data] => Hallo blog readers
)
)
[_data] =>
)
=========================================================================
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:
=========================================================================
Script
./whatsapp_whatsapi_listen.php
[07/07/2013 15:57] From: 491231234567, Name: Philipp, Message: Hallo blog readers
[07/07/2013 15:57] From: 491231234567, Name: Philipp, Message: Everything I write is printed to STDOUT
[07/07/2013 15: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.
=================By:-
ALL 3G HACKING TEAM===================