我们使用以下代码通过 WhatsApp Cloud API 发送 WhatsApp 消息。代码如下:
<?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
}
?>
我们遇到的问题是消息会发送到某些号码,但不会发送到其他号码。在某些情况下,仅传送多条消息中的一条。我们确信这些号码是正确的,并且它们已在 WhatsApp 上注册。
请协助我们尽快解决此问题。
谢谢你。
我们遇到的问题是消息会发送到某些号码,但不会发送到其他号码。
您的情况的原因可能会有所不同,
1-您没有与您尝试联系的号码进行公开会话,如果您没有与他们进行公开对话,您只能使用批准的模板发送消息。
2-您的waba受到限制,请检查您的业务经理是否有任何限制消息。
3- 您没有从业务经理处将付款方式连接到您的 Waba。
建议您始终向 Whatsapp 合作伙伴注册您的 WABA,这样您就可以获得他们@Karzoun 的支持。