WhatsApp Business API – 无法创建消息模板

huangapple go评论64阅读模式
英文:

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&amp;name=compra_terminada&amp;category=MARKETING&amp;language=pt_PT&amp;components=[{&quot;type&quot;:&quot;BODY&quot;,&quot;text&quot;:&quot;Obrigado por comprar na nossa loja. Assim que o objecto for expedido, ir&#225; receber no seu email o numero de rastreamento da encomenda.&quot;},{&quot;type&quot;:&quot;FOOTER&quot;,&quot;text&quot;:&quot;A minha empresa&quot;}]&quot;
</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 = &#39;https://graph.facebook.com/v16.0/&#39; . $businessaccountid . &#39;/message_templates?allow_category_change=true&amp;name=&#39; . $templateName . &#39;&amp;category=&#39; . $templateCat . &#39;&amp;language=&#39; . $language . &#39;&amp;components=&#39; . $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[] = &#39;Authorization: Bearer &#39; . $token ;
	curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
	
	

	$result = curl_exec($ch);
	

	if ($result === false) {
		$result = curl_error($ch) . &quot; - &quot;.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&#39;re working on it and we&#39;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 = &#39;https://graph.facebook.com/v15.0/&#39; . $businessaccountid . &#39;/message_templates&#39;;

	$data = array(
		&#39;allow_category_change&#39; =&gt; true,
		&#39;name&#39; =&gt; $templateName,
		&#39;category&#39; =&gt; $templateCat,
		&#39;language&#39; =&gt; $language,
		&#39;components&#39; =&gt; $components
	);
	$headers = array();
	
	$headers[] = &#39;Authorization: Bearer &#39; . $token ;
	$headers[] = &#39;Content-Type:application/json&#39; ;

	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) . &quot; - &quot;.curl_errno($ch);
        
    }else{
        $resultDecode = json_decode($result);
        if($resultDecode!=null){
            $result = $resultDecode;
        }
        return($result);
    }
    curl_close($ch);
    
}

huangapple
  • 本文由 发表于 2023年4月6日 23:53:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75951466.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定