我正在尝试使用 Whatsapp Cloud API 发送 WhatsApp 模板消息。我收到此错误:
"error": {
"message": "(#100) The parameter messaging_product is required.",
"type": "OAuthException",
"code": 100,
"fbtrace_id": "AcwogNVBOUBxkwsF7NkMiWj"
}
我转换为 PHP 并通过 cURL post 发送的请求是:
// Template parameters
$header_text_string = "This is a header";
$text_string = "this is the body";
$footer_text_string = "This is a footer";
// API endpoint
$phone_number_id = "...";
$api_url = 'https://graph.facebook.com/v17.0/' . $phone_number_id . '/messages';
// Function to send an HTTP POST request using cURL
function sendHttpPostRequest($url, $data, $access_token) {
$headers = array(
'Content-Type: application/json',
'Authorization: Bearer ' . $access_token,
);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
if ($response == false) {
$result = "cURL Error: " . curl_error($ch) . " - ".curl_errno($ch);
return($result);
}else{
$resultDecode = json_decode($response, false);
if($resultDecode != null){
$result = $resultDecode;
}
return($result);
}
curl_close($ch);
}
// Send WhatsApp template messages to each recipient
foreach ($phoneNumbers as $recipient_phone_number) {
$template_data = array(
'messaging_product' => 'whatsapp',
'recipient_type' => 'individual',
'to' => $recipient_phone_number,
'type' => 'template',
'template' => array(
'name' => $template_name,
'language' => array(
'code' => 'en_US'
),
'components' => array(
array(
"type" => "header",
"parameters" => array(
array(
"type" => "text",
"text" => $header_text_string
)
)
),
array(
'type' => 'body',
'parameters' => array(
array(
'type' => 'text',
'text' => $text_string
)
)
)
)
)
);
// Convert the template data to JSON
$json_data = json_encode($template_data);
// Send the HTTP POST request to the API endpoint
$response = sendHttpPostRequest($api_url, $json_data, $access_token);
// Convert the response object to a JSON string to debug if needed
$response_json = json_encode($response);
// Output the JSON response
echo "API Response: " . $response_json . "<br>";
}
我环顾四周,发现非常相似或有些相关的帖子:here、here、here等,但我似乎有我的代码,因为它在答案中,我仍然得到这个。我错过了什么?
另外,如果重要的话,现在我正在用邮递员测试它(根据需要设置标题和所有内容,但仍然什么也没有)。