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. 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.

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

Code for Send the messages

Here, we have provide the full code to send whatsapp messages in someone’s using PHP.

<?PHP
$token = ""; // PHPHive WhatsAPI Token, Get it from http://wapi.phphive.info
$wa_uid = ""; // WhatsApp Username
$wa_pwd = ""; // WhatsApp Password
$wa_recp = ""; // recipient
$wa_msg  = ""; // Message You want to Send

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://wapi.phphive.info/api/message/send.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,"token=".$token."&wa_uid=".$wa_uid."&wa_pwd=".$wa_pwd."&wa_recp=".$wa_recp."&wa_msg=".urlencode($wa_msg));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);
echo $server_output;
?>

Given the Sample Output (Send message)

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

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

Code for Receive the messages

Here, we have provide the full code to receive whatsapp messages in someone’s using PHP.

<?PHP
$token = ""; // PHPHive WhatsAPI Token, Get it from http://wapi.phphive.info
$wa_uid = ""; // WhatsApp Username
$wa_pwd = ""; // WhatsApp Password

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://wapi.phphive.info/api/message/receive.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,"token=".$token."&wa_uid=".$wa_uid."&wa_pwd=".$wa_pwd);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);
echo $server_output;
?>

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 messages.

$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.;
}