How to Sync Contacts in WhatsApp using PHP
Category: WhatsAPI
In this tutorial you will see How to Sync Contacts in WhatsApp using PHP. Syncing Contacts is an Initial & even Required Step. 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 ]
Prerequisite’s for Syncing Contacts in WhatsApp :-
- WhatsAPI PHP Library.
- WhatsApp Password.
If You haven’t Download WhatsAPI PHP Library, so Download it Now as we gonna use it in the Process of Obtaining WhatsApp Password.
Download “WhatsAPI.zip” master.zip – Downloaded 57383 times –
If You Dont Have WhatsApp Password, Refer this :- How to Get WhatsApp Password
So Let’s Get Started !
Logging in to WhatsApp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
require_once("src/whatsprot.class.php"); $username = ""; // WhatsApp Username $password = ""; // WhatsApp Password $wa = new WhatsProt($username, "WhatsApp", true); try { $wa->connect(); $wa->loginWithPassword($password); } catch(Exception $e) { echo "ERROR : Login Failed"; exit(0); } |
Syncing Contacts
1 2 3 4 5 6 7 8 |
$numbers = array("91XXXXXXXXX","92XXXXXXXX"); // You can Add any number you want to Sync in $numbers array //sending Sync Request to Server $wa->sendSync($numbers); //wait for response from Server while ($wa->pollMessage()); |
Using Events to Check Whether a Particular Number Exists on WhatsApp or Not
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
//bind event handler after Initializing WhatsProt Handler ( $wa ) $wa->eventManager()->bind('onGetSyncResult', 'onSyncResult'); //event handler /** * @param $result SyncResult */ function onSyncResult($result) { foreach ($result->existing as $number) { echo "$number exists<br />"; } foreach ($result->nonExisting as $number) { echo "$number does not exist<br />"; } } |
Complete Code :-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
require_once 'src/whatsprot.class.php'; $username = ""; // WhatsApp Username $password = ""; // WhatsApp Password $numbers = array("91XXXXX","92XXXXX"); //event handler /** * @param $result SyncResult */ function onSyncResult($result) { foreach ($result->existing as $number) { echo "$number exists<br />"; } foreach ($result->nonExisting as $number) { echo "$number does not exist<br />"; } die(); //to break out of the while(true) loop } $wa = new WhatsProt($username, 'WhatsApp', false); //bind event handler $wa->eventManager()->bind('onGetSyncResult', 'onSyncResult'); try { $wa->connect(); $wa->loginWithPassword($password); } catch(Exception $e) { echo "ERROR : Login Failed"; exit(0); } //sending Sync Request to Server $wa->sendSync($numbers); //wait for response from Server while ($wa->pollMessage()); |
Download “WhatsAppSyncContacts.zip” WhatsAppSyncContacts.zip – Downloaded 1837 times – 818 B
Thanks!
Your feedback helps us to improve PHPHive.info
3 Comments
but hw to send and receive messages ?
please tell how to send and receive messages ? give me link for that tutorial
Refer this : http://www.phphive.info/285/how-to-send-and-receive-messages-in-whatsapp-using-php/