wechat-php-sdk icon indicating copy to clipboard operation
wechat-php-sdk copied to clipboard

请问如何使用模板消息函数?

Open byenic opened this issue 9 years ago • 1 comments

$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();

这样的例子对吗?

byenic avatar Jun 26 '15 19:06 byenic

不需要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;
    }

nfer avatar Dec 22 '15 15:12 nfer