PHP Script to send and receive messages on WhatsApp

PHP Script to send and receive messages on

WhatsApp

I have been reading many tutorials for sending and receiving WhatsApp Messages via PHP but there is not a single Article Which explains properly with  Steps that How To retrieve Your WhatsApp password Which is created and Stored when you create a WhatsApp Account on the WhatsApp Server which is the main Challenge as of now in other Articles.

Thanks To this tool Which has Made 50% of the work Easy. https://github.com/shirioko/WART

Okay So we will Quickly go through the Steps for WhatsApp Registration Tool :

  1. Go To the link https://github.com/shirioko/WART
  2. Download The Whole project by Clicking Clone to Desktop
  3. Run the WART-1.7.3.0 Exe File

 

 

 

WART

 

Enter Phone Number with Country Code (e.g. India Number : 91xxxxxxxxxx)

P.s. Do not put any Special Symbol like (+91) for India

 

Click on Request Code and You will Receive a Code from WhatsApp on the Entered Mobile Number.

 

Now Verify that Code in 2nd Step and That’s it . You are here . Your password will Appear.

P.s. Do not Share your password with Anyone Because it Can be used in loop for any Kind of messages.

okay Now we are done with 50% of the task .

Now we need to write the php Script Which will Send Messages to WhatsApp Registered Number.

You will need 2 things for Sending Messages to WhatsApp through PHP :

  1. Username (WHICH IS YOUR MOBILE NUMBER WITH COUNTRY CODE) : 91xxxxxxxxxxx
  2. Password (Which You got From The WART.EXE TOOL

Alright Sparky Lets Get this Done…..

 

<?php
require_once ‘./src/whatsprot.class.php’;
$username = “919xxxxxxxxx”; //Mobile Phone prefixed with country code so for india it will be 91xxxxxxxx
$password = “your password”;

$w = new WhatsProt($username, 0, “Mayank Grover Blog”, true); //Name your application by replacing “WhatsApp Messaging”
$w->connect();
$w->loginWithPassword($password);

$target = ’91xxxxxxxxxxx’; //Target Phone,reciever phone
$message = ‘Hello User !! This is a Tutorial for sending messages via php to WhatsApp Account’;

$w->SendPresenceSubscription($target); //Let us first send presence to user
$w->sendMessage($target,$message ); // Send Message
echo “Message Sent Successfully”;
?>

So Everything is Quite Clear in the above Script i.e. You need to set your Application Details Username as Mobile Number password Which you got from WART Tool Target as your Target Mobile Number and Lastly The message(Plain Text Which you need to send).

 

Okay Now I know many Questions are Coming in your mind.

  1. In first line Require src/whatsprot.class.php What is This. ???

You Need to Download the WhatsApi from this Link . There You Will find the src

directory with all the source files.

  1. Can we only Send Plain Text messages Or media like Images and Videos???

Yes we can send media file Also.

So Next I will be writing php script to send Media file To any WhatsApp Account.

 

If You like This Article Then please Share This.

 

53 thoughts on “PHP Script to send and receive messages on WhatsApp

    1. Beleh : This is the password Which is Stored in WhatsApp database Corresponding to Your Mobile Number. So Whatever Unique About your Mobile Number That WhatsApp has is this password. Earlier This Password was MD5 hash of your IMEI (or MAC address), But Now It has Changed.
      And as far as Decryption of Whatsapp.crypt.8 is concerned I am not sure about that may be its only One way encryption

      Like

      1. i tried using this key on to decrypt the backup it didn’t work, but i’m sure there is a way to generate the key from this password

        Like

  1. I have been Observing many Comments on Facebook that WART tool bans Your WhatsApp and all that.
    So Let me tell you all WhatsApp Verifies Identity by message or by Voice call. So if you are using your Regular Mobile Number for WhatsApp Api php then It will log you out of Your WhatsApp from Mobile and will Ask you to verify Your number Again . So for One Number You can use only for one purpose either in mobile phone or In WhatsApp Api PHP . And there is nothing Like Ban on your WhatsApp Account or loss of Your Chats on WhatsApp.
    So If you want to use WhatApp Api PHP Then I would Suggest To use it with an alternate Number.
    Thanks.

    Like

  2. is it possible to throw appropriate error notice if message delivery was unsuccessful..
    btw thanks a ton for the API 🙂

    Like

    1. pradeep gajanoor : In the following Script

      require_once ‘./src/whatsprot.class.php’;
      $username = “919xxxxxxxxx”; //Mobile Phone prefixed with country code so for india it will be 91xxxxxxxx
      $password = “your password”;

      $w = new WhatsProt($username, 0, “Mayank Grover Blog”, true); //Name your application by replacing “WhatsApp Messaging”
      $w->connect();
      $w->loginWithPassword($password);

      $target = ’91xxxxxxxxxxx’; //Target Phone,reciever phone
      $message = ‘Hello User !! This is a Tutorial for sending messages via php to WhatsApp Account’;

      $w->SendPresenceSubscription($target); //Let us first send presence to user
      $w->sendMessage($target,$message ); // Send Message

      Now You have all the response in $w Array.

      You just have to Print_r($w); and You will get all the response which you can filter according to your Use.

      Thanks.

      Like

      1. i’ve implemented print_r($w) to see if the message delivered successfully but i seen hundreds of array elements don’t know which one to choose from..could you please help me with that? 🙂
        thank you

        Like

      2. The code are executing successfully but i’m getting some weird output on the screen(i’ve attached a screenshot), how i’m suppose to get rid of it?
        Thank you 🙂

        Like

  3. Fatal error: Uncaught exception ‘Exception’ with message ‘Login Failure’ in C:wampwwwWhatsAPI-mastersrcwhatsprot.class.php:1640 Stack trace: #0 C:wampwwwWhatsAPI-mastersrcwhatsprot.class.php(474): WhatsProt->doLogin() #1 C:wampwwwwhatsapp.php(8): WhatsProt->loginWithPassword(‘hYDrWCJDwEdtlGu…’) #2 {main} thrown in C:wampwwwWhatsAPI-mastersrcwhatsprot.class.php on line 1640

    Like

    1. @ernestjnrmuroiwa:disqus : Sir as per My Understanding you are running whatsapp.php file which asks you to enter the password and then it gives the error because in that file source the username and other details are hardcoded means you need to change those before use . However If you want to See this Script in Action Quickly then You can create a test.php file and paste the above code by altering it with your credentials.
      Thanks.

      Like

      1. @mnkgrover08 thank you for the reply
        i changed credntials to the code above and it gave me that fatal error her is the code that is giving me an error
        connect();
        $w->loginWithPassword($password);

        $target = ‘263773755300’; //Target Phone,reciever phone
        $message = ‘Hello User !! This is a Tutorial for sending messages via php to WhatsApp Account’;

        $w->SendPresenceSubscription($target); //Let us first send presence to user
        $w->sendMessage($target,$message ); // Send Message
        echo “Message Sent Successfully”;
        ?>

        Like

      2. @ernestjnrmuroiwa:disqus : Sir in this Case please provide your Skype or Gmail Address , So that I can look Clearly into That .. Because I have tested this on my machine and its running fine.

        P.s. Once try to use this on any server or public IP rather than Localhost. Thanks.

        My Gmail Address is : mnkgrover08@gmail.com

        Like

      3. the problem i noticed is that i tried to use my account which was concurrently active one my mobile. Used another number and it worked fine. Can not wait for the other tutorials on how to receive messages
        Thank you very much

        Like

    1. @bhaskarb79:disqus : Please make a seperate php file with the above code and then try. Do not try whatsapp.php file or else change the other credentials in that file source which are hardcoded.
      Thanks.

      Like

      1. @bhaskarb79:disqus : Are You Using Localhost ( WAMP or XAMPP) or any Public Server .

        Please try with Public Server or IP.

        Thanks.

        Like

      2. @bhaskarb79:disqus : May Be Then I need to look into your system Because i tested this on many systems its running fine. My Email is mnkgrover08@gmail.com

        Send me your server Details then I will let you now.

        Thanks.

        Like

      1. @prateek_anand:disqus : I have already Posted the Solution in Above Article . However if You are not able to run this then I can help only when I can look into your code.

        Thanks.

        Like

  4. Hi Maynak, Can we use this script as a Bulk Sending. As an Example We do have a online store and We need to send promotional items via Whatsapp. Can we do it using this script ?

    Like

    1. Yes 4 sure you can use this for bulk mail but if someone reports your number then your whatsapp will be blocked on that number..
      So the alternative is to take around 400 – 500 numbers and rotate them and distribute the number of messages by running a cron job and also you may require to rotate your IP also

      Like

      1. can please share how to switch sim number and IP.??
        i was tried to switch numbers but no luck….
        so please share your experience in bulk script
        Thanks

        Like

  5. i’ve implemented print_r($w) to see if the message delivered successfully but i seen hundreds of array elements don’t know which one to choose from..could you please help me with that? 🙂
    thank you

    Like

  6. Security Question: How do we know if at the of the line there is a bot (call me mitm or whatever) that stores our passwords too…

    Like

    1. @andreas : yes it can be that the Wart tool which we are using for retrieving our password is storing our password and acting as a man in the middle .. And maybe the wart tool creator uses this for bulk messaging …. Anywayz that tool is present on github so they must have checked the exe for any malware Trojan or anything else… I will do a research on this using wireshark network and will suspect if any our information is going to his virtual machine

      Like

  7. I am receiving messages..but it is showing some xml statements when sendMessage() is called..how can i hide those xml statements.

    Like

  8. Nice Tutorial.

    Thanks for sharing this with us.

    Can you please guide us related to receiving whats app message and save that messages in database.

    Like

Leave a comment