We are using the following code to send WhatsApp messages using the WhatsApp Cloud API. The code is as follows:
<?php
$phoneNumberId = 'Phone number ID';
$accessToken = 'Access Token';
// Array of phone numbers
$recipientPhoneNumbers = [
'number 1',
'number 2',
'number 3',
'number 4',
];
$text = "
Hello,
Thank you for registering with Co Network. To activate your account and benefit from our services, please click on the following link:
Link
Thank you for choosing Co Network.";
$url = "https://graph.facebook.com/v20.0/$phoneNumberId/messages";
// Loop through each number in the array and send the message
foreach ($recipientPhoneNumbers as $recipientPhoneNumber) {
$data = [
"messaging_product" => "whatsapp",
"to" => $recipientPhoneNumber,
"type" => "text",
"text" => [
"body" => $text
]
];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Authorization: Bearer ' . $accessToken
]);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$result = curl_exec($ch);
if (curl_errno($ch)) {
$error_msg = curl_error($ch);
curl_close($ch);
error_log('Error sending message to ' . $recipientPhoneNumber . ': ' . $error_msg);
echo 'Error sending message to ' . $recipientPhoneNumber . ': ' . $error_msg . '<br>'; // Display error message directly on the screen
continue; // Skip to the next number in case of an error
}
curl_close($ch);
$response = json_decode($result, true);
if (isset($response['error'])) {
error_log('Error: ' . $response['error']['message']);
echo 'Error: ' . $response['error']['message'] . ' for ' . $recipientPhoneNumber . '<br>'; // Display error message directly on the screen
var_dump($response); // Display the full response for more accurate diagnosis
continue; // Skip to the next number in case of an error
}
error_log('Message sent successfully to ' . $recipientPhoneNumber);
echo 'Message sent successfully to ' . $recipientPhoneNumber . '<br>'; // Display success message directly on the screen
var_dump($response); // Display the full response to confirm success
}
?>
The issue we are encountering is that messages are delivered to some numbers but not to others. In some cases, only one message out of many is delivered. We are certain that the numbers are correct and that they are registered on WhatsApp.
Please assist us in resolving this issue as quickly as possible.
Thank you.
The issue we are encountering is that messages are delivered to some numbers but not to others.
$result
contain?$response
variable for numbers whose message is not delivered, what do you have...? Content identical to a message sent...?