英文:
WhatsApp Business Api - Can't create Message Template
问题
I'm having trouble in creating a WhatsApp Message template, using the WhatsApp Business API.
I already can send messages, delete template, etc.
Creating the message template is the only API implementation where I'm having some difficulties.
This is the code that I'm using to create a template:
public static function CreateTemplate($templateName, $templateCat, $language, $components){
$businessaccountid = .....";
$token = ....;
$ch = curl_init();
$url = 'https://graph.facebook.com/v16.0/' . $businessaccountid . '/message_templates?allow_category_change=true&name=' . $templateName . '&category=' . $templateCat . '&language=' . $language . '&components=' . $components;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
$headers = array();
$headers[] = 'Authorization: Bearer ' . $token ;
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if ($result === false) {
$result = curl_error($ch) . " - ".curl_errno($ch);
} else {
$resultDecode = json_decode($result);
if($resultDecode != null) {
$result = $resultDecode;
}
return $result;
}
curl_close($ch);
}
And this is the content of the $url variable that is being used:
<pre>
"https://graph.facebook.com/v16.0/....../message_templates?allow_category_change=true&name=compra_terminada&category=MARKETING&language=pt_PT&components=[{"type":"BODY","text":"Obrigado por comprar na nossa loja. Assim que o objecto for expedido, irá receber no seu email o numero de rastreamento da encomenda."},{"type":"FOOTER","text":"A minha empresa"}]"
</pre>
And this is the error that it's returning from curl_error and curl_errno:
HTTP/2 stream 0 was not closed cleanly: Unknown error code (err 5) - 92
I've already tried to use CURL_HTTP_VERSION_1_1 but with no success. The curl_exec($ch) returns a generic html page from Facebook saying:
Sorry, something went wrong.
We're working on it and we'll get it fixed as soon as we can.
What do I need to change in my code to create a WhatsApp Template Message?
英文:
I'm having trouble in creating a WhatsApp Message template, using the WhatsApp Business API.
I already can send messages, delete template, etc.
Creating the message template is the only API implementation where I'm having some dificulties.
This is the code that I'm using to create a template:
public static function CreateTemplate($templateName, $templateCat, $language, $components){
$businessaccountid = .....;
$token = ....;
$ch = curl_init();
$url = 'https://graph.facebook.com/v16.0/' . $businessaccountid . '/message_templates?allow_category_change=true&name=' . $templateName . '&category=' . $templateCat . '&language=' . $language . '&components=' . $components;
//echo($url);
//curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
//curl_setopt($ch, CURLOPT_VERBOSE , TRUE);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
$headers = array();
$headers[] = 'Authorization: Bearer ' . $token ;
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if ($result === false) {
$result = curl_error($ch) . " - ".curl_errno($ch);
}else{
$resultDecode = json_decode($result);
if($resultDecode!=null){
$result = $resultDecode;
}
return($result);
}
curl_close($ch);
}
And this is the content of the $url variable that is being used:
<pre>
"https://graph.facebook.com/v16.0/....../message_templates?allow_category_change=true&name=compra_terminada&category=MARKETING&language=pt_PT&components=[{"type":"BODY","text":"Obrigado por comprar na nossa loja. Assim que o objecto for expedido, irá receber no seu email o numero de rastreamento da encomenda."},{"type":"FOOTER","text":"A minha empresa"}]"
</pre>
And this is error that it's returning from curl_error and curl_errno:
HTTP/2 stream 0 was not closed cleanly: Unknown error code (err 5) - 92
I've already tried to use CURL_HTTP_VERSION_1_1 but with no success. The curl_exec($ch) returns a generic html page from Facebook saying:
Sorry, something went wrong.
We're working on it and we'll get it fixed as soon as we can.
What do I need to change in my code to create a WhatsApp Template Message?
答案1
得分: 0
这个问题通过将参数作为 JSON 字符串发送,并将头部设置为 "application/json" 来解决。
英文:
This issue was solved by sending the parameters as a json string, and setting the header to "application/json"
public static function CreateTemplate($templateName, $templateCat, $language, $components){
$businessaccountid = .....;
$token = ....;
$ch = curl_init();
$url = 'https://graph.facebook.com/v15.0/' . $businessaccountid . '/message_templates';
$data = array(
'allow_category_change' => true,
'name' => $templateName,
'category' => $templateCat,
'language' => $language,
'components' => $components
);
$headers = array();
$headers[] = 'Authorization: Bearer ' . $token ;
$headers[] = 'Content-Type:application/json' ;
curl_setopt($ch, CURLOPT_URL, $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);
$result = curl_exec($ch);
if ($result === false) {
$result = curl_error($ch) . " - ".curl_errno($ch);
}else{
$resultDecode = json_decode($result);
if($resultDecode!=null){
$result = $resultDecode;
}
return($result);
}
curl_close($ch);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论