fbpx

PHP SEND METHOD

Just add following code in your PHP file to start sending WhatsApp messages from your scripts.
define(“HOST”“Your Admin Panel URL”);
define(“EMAIL”“Your Admin Panel Email”);
define(“PASSWORD”“Your Admin Panel Password”);
/**
 * @param string $number Mobile Number where you want to send message
 * @param string $message Message you want to send
 * @param int $device ID of the device you want to use to send this message
 * @return bool|string Error message if there is an error otherwise false
 */
function SendSingleMessage($number$message$device 0)
{
    $url HOST “/services/send.php”;
    $postData array(‘messages’ => json_encode([[‘number’ => $number‘message’ => $message]]), ’email’ => EMAIL‘password’ => PASSWORD‘devices’ => $device);
    return SendRequest($url$postData);
}
/**
 * @param array $messages Array containing numbers and messages
 * @param bool $useAvailable Set it to true if you want to use all available devices to send these messages.
 * @param array $devices Array of ID of devices you want to use to send these messages
 * @return bool|string Error message if there is an error otherwise false
 */
function SendMessages($messages$useAvailable true$devices = [])
{
    $url HOST “/services/send.php”;
    $postData array(‘messages’ => json_encode($messages), ’email’ => EMAIL‘password’ => PASSWORD‘devices’ => json_encode($devices), ‘useAvailable’ => $useAvailable);
    return SendRequest($url$postData);
}
function SendRequest($url$postData)
{
    $ch curl_init();
    curl_setopt($chCURLOPT_URL$url);
    curl_setopt($chCURLOPT_POSTtrue);
    curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
    curl_setopt($chCURLOPT_POSTFIELDShttp_build_query($postData));
    $response curl_exec($ch);
    $httpCode curl_getinfo($chCURLINFO_HTTP_CODE);
    if (curl_errno($ch)) {
        return curl_error($ch);
    }
    curl_close($ch);
    if ($httpCode == 200) {
        $json json_decode($responsetrue);
        if($json == false) {
            if(empty($response)) {
                return “Missing data in request. Please provide all the required information to send messages.”;
            } else {
                return $response;
            }
        }
        else if (!$json[“success”]) {
            return $json[“error”][“message”];
        }
    } else {
        return “HTTP Error Code : {$httpCode};
    };
    return false;
}
You can use these functions to send single or multiple messages as shown below.
If you want to send only one message to single mobile number then you should use the following code. Do not use this function if you want to send multiple messages, You should use SendMessages function in such a case. If you want to specify device you want to use to send messages then you need a ID of the device. You can find ID of the device using this guide.
// Send messages using device ID 1.
//$error = SendSingleMessage(“+911234567890”, “This is a test of single message.”, 1);
//Send message using primary device.
$error = SendSingleMessage(“+911234567890”“This is a test of single message”);
if ($error) {
    echo $error;
else {
    echo “Successfully sent single message.”;
}
If you want to send multiple messages to different mobile numbers then you should use the following code.
$messages array();
for ($i 1$i <= 4$i++) {
    array_push($messages,
        [
            “number” => “+911234567890”,
            “message” => “This is a test #{$i} of PHP version. Testing bulk message functionality.”
        ]);
}
// Send messages using only primary device.
$error = SendMessages($messagesfalse);
if ($error) {
    echo $error;
else {
    echo “Successfully sent bulk messages.”;
}
// Send messages using all devices. Messages will be split between all available devices.
//$error = SendMessages($messages, true);
// Send messages using only specified devices. Messages will be split between devices you specified.
// If you send 10 messages using this code then 5 messages will be sent by Device #1 and others will be sent by Device #2.
//$error = SendMessages($messages, false, [1, 2]);