wechat-php-sdk
wechat-php-sdk copied to clipboard
请问如何使用模板消息函数?
$weObj = new Wechat($options);
$command=$weObj->getRevContent();
if($command=='mes'){
$template=array(
'touser'=>"oiiOgs64yIXERkWWXuyN6Uf4hDUc", 'template_id'=>"lPHndgN3zf1HlKhHT1T3_VZgKAawYjRUdrWwOuY1qdQ",
'url'=>"http://weixin.qq.com/download",
'topcolor'=>"#FF0000",
'data'=>array(
'first'=>array('value'=>"gongxi",'color'=>"#00008B"),
'keyword1'=>array('value'=>"gongxi",'color'=>'#00008B'),
'keyword2'=>array('value'=>"gongxi",'color'=>'#00008B'),
'keyword3'=>array('value'=>"gongxi",'color'=>'#00008B'),
'keyword4'=>array('value'=>"gongxi",'color'=>'#00008B'),
'remark'=>array('value'=>"gongxi",'color'=>'#00008B'),
)
);
$weObj->sendTemplateMessage($template)->reply();
这样的例子对吗?
不需要reply()的,详情请参考源代码:
public function sendTemplateMessage($data){
if (!$this->access_token && !$this->checkAuth()) return false;
$result = $this->http_post(self::API_URL_PREFIX.self::TEMPLATE_SEND_URL.'access_token='.$this->access_token,self::json_encode($data));
if($result){
$json = json_decode($result,true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return false;
}
return $json;
}
return false;
}
注意,这里并没有返回$this
,因此不支持链式操作。
而且,在调用getRevContent()
之前,需要先调用getRev()
,这个从源码上也可以看出来:
/**
* 获取接收消息内容正文
*/
public function getRevContent(){
if (isset($this->_receive['Content']))
return $this->_receive['Content'];
else if (isset($this->_receive['Recognition'])) //获取语音识别文字内容,需申请开通
return $this->_receive['Recognition'];
else
return false;
}