Send and Receive WhatsApp messages using PHP

Send and Receive WhatsApp messages using PHP – This tutorial will explain how to send and receive whatsapp messages using php. Here you have learn the full of techniques to send the whatsapp messages. You Need to sync Contacts with WhatsApp Server to Send Messages to those Numbers.

Already you know the facts like every person has Whatsapp application. So we easily communicate the message with help of WhatsApp service. Therefore first we have to enable the API for send and receive messages from end users. Without API we can’t send messages from our friends, colleagues etc.

For that follow the steps then easily transferred messages from anyone who are install the application. Otherwise it’s not executed, so in below I have explained the steps for activate the WhatsApp API service for developers and end users.

Note : If You try to Send Messages without Syncing Contacts , your WhatsApp may Get BLOCKED.

send and receive whatsapp messages using php

Here, we need two objects to execute the script so just get the link and learn full thing to run the script. Below I have provide the requirements link so just see and go on the link to execute it. Moreover you can also able to send and receive whatsapp messages using Python code. Because our developers are always update new things.

Nowadays number of opportunities are available to make money online without any investments. Apart from this article you can easily make money online like Earn Money  $10 Per Day.

Send and Receive WhatsApp messages using PHP

In this tutorial you will see How to Send and Receive Messages in WhatsApp using PHP. Syncing Contacts is an Initial & even Required Step. You Need to sync Contacts with WhatsApp Server to Send Messages.

Prerequisite

There are two prerequisite there to execute the script. They are,
1. PHPHive WhatsApp RESTAPI Token
Click here to get the token WhatsApp API Token.
2. WhatsApp Password
Just refer this link to get the password Get WhatsApp Password

Given the Sample Output (Send message)

This is sample output for above code to send the messages. In below I have add the same screenshot for how we send whatsapp message using PHP & MySQL. For that we have lot of libraries file are available to make the integration.

{
    "request": true,
    "response": true,
    "output": "Message Sent",
    "msg_sent_today": 1
}

Given the Sample Output (Receive message)

This is sample output for above code to send the messages.

{
    "request": true,
    "response": [
        {
            "mynumber": "WhatsApp Username",
            "fromNo": "XXXXXXXXXX",
            "fromName": "vetripandi",
            "msgId": "D799FF10BED49CD9F9",
            "msgType": "text",
            "msgTime": "1469001844",
            "msg": "Hey"
        },
        {
            "mynumber": "WhatsApp Username",
            "fromNo": "XXXXXXXXX",
            "fromName": "vetripandi",
            "msgId": "3510107294D97960F0",
            "msgType": "text",
            "msgTime": "1469001844",
            "msg": "Hi"
        }
    ]
}

There’s no daily limits to send and receive messages. But spamming is obviously not allowed. We are keeping records of IP’s, if we find someone spamming our service then that IP will be Banned.

Prerequisite’s for Syncing Contacts in WhatsApp

1. WhatsAPI PHP Library.
2. WA Password.
3. PHP Protobuf and Curve25519 to enable end to end encryption.
If You dont have WA Password, just refer this article Get WhataApp Password
If You haven’t download WhatsAPI PHP Library, so download it now as we going to use in the Process of obtaining WA Password.

Click to download

Logging on WhatsApp

Include the library file and save the code.

require_once("src/whatsprot.class.php");

$username = ""; // Username
$password = ""; // Password

$wa = new WhatsProt($username, "WhatsApp", true);

try {
$wa->connect();
$wa->loginWithPassword($password);
  } catch(Exception $e) {
    echo "ERROR : Login Failed";
    exit(0);
}

Send the messages

Here, the full code of send the whatsapp messages. And I hope below codes are helps to communicate with whatsapp application.

$no=""; // Number
$msg=""; // Message

try {
      $wa->sendMessage($no, $msg);
      echo 'Text Message Sent';
 } catch(Exception $e) {
      echo "ERROR : Text Message Sending Failed";
}

Receive the messages

Here, the full code of receive the messages.

// Bind Event onGetMessage
$wa->eventManager()->bind("onGetMessage", "onGetMessage");

// onGetMessage Event Handler Function
// Modify it According to your Needs
function onGetMessage($mynumber, $from, $id, $type, $time, $name, $body) {
         echo 'Message Received';		
	 echo "DEBUG : onGetMessage";
	 echo "mynumber : ".$mynumber.;
	 echo "from : ".$from.;
	 echo "id : ".$id.;
	 echo "type : ".$type.;
	 echo "name : ".$name.;
	 echo "body : ".$body.;
}

Complete code Send & Receive messages

Here, we provide the complete code.

require_once 'src/whatsprot.class.php';

$username = ""; // Username
$password = ""; // Password

$numbers = array("91XXXXX","92XXXXX");

//event handler
/**
 * @param $result SyncResult
 */
function onSyncResult($result)
{
    foreach ($result->existing as $number) {
        echo "$number exists";
    }
    foreach ($result->nonExisting as $number) {
        echo "$number does not exist";
    }
    die(); //to break out of the while(true) loop
}
$wa = new WhatsProt($username, 'WA', false);

// Bind Event onGetMessage
$wa->eventManager()->bind("onGetMessage", "onGetMessage");

try {
$wa->connect();
$wa->loginWithPassword($password);
  } catch(Exception $e) {
    echo "ERROR : Login Failed";
    exit(0);
}

$no=""; // Number
$msg=""; // Message

try {
      $wa->sendMessage($no, $msg);
      echo 'Text Message Sent';
 } catch(Exception $e) {
      echo "ERROR : Text Message Sending Failed";
}

//wait for response from Server
while ($wa->pollMessage());

// onGetMessage Event Handler Function
// Modify it According to your Needs
function onGetMessage($mynumber, $from, $id, $type, $time, $name, $body) {
         echo 'Message Received';		
	 echo "DEBUG : onGetMessage";
	 echo "mynumber : ".$mynumber.;
	 echo "from : ".$from.;
	 echo "id : ".$id.;
	 echo "type : ".$type.;
	 echo "name : ".$name.;
	 echo "body : ".$body.;
}

If you have any doubts regarding this just comment below I will response your queries within a days. Because we are giving main priority for all of existing and new readers.

Leave a Reply