使用WhatsApp Cloud API发送模板消息报错:Unexpected key "0" on param "template"
解决WhatsApp Cloud API模板消息报错:Unexpected key "0" on param "template". [type]
错误原因
你的请求中template.components的结构不符合API要求。当前写法把header和body分别放在两个独立的数组里,导致components变成了包含两个子数组的二维数组,而WhatsApp API要求components是直接包含各个组件对象的一维数组。
修正后的代码
"messaging_product" => "whatsapp", "recipient_type" => "individual", "to" => "$to_number", "type" => "template", 'template' => array( "name" => "templateName", 'language' => array("code" => "en"), 'components' => array( array( "type" => "header", "parameters" => array( array( "type" => "image", "image" => array( "link" => $imageLink ) ) ) ), array( "type" => "body", "parameters" => array( array("type" => "text","text" => $Productid), ) ) ) )
关键修改点
- 移除了
components下header和body外层多余的数组包裹,让两个组件直接成为components数组的元素。 - 确保
components是一维数组,每个元素对应一个模板组件(header、body、button等)。
内容的提问来源于stack exchange,提问作者crazyboy24
相关产品推荐
相关产品推荐

