微信消息模板推送

首先在公众号里面添加模板 功能-》添加功能插件-》模板消息

接下来我们就看看如何发送模板消息:
这个是官方文档:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1433751277

为了更方便,我会直接将相关代码贴出来。
http请求方式: POST
https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN
这里我们首先需要的就是access_token了,这个在这里就不多说了。通过你的appid和secret就可以获取。

    获取access_token :  
             $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$secret";
            $token =$this->getJson($url);
            $token = json_decode($token, true);
                            $token=$token['access_token'];  //获取token 也可以在此做数据库操作

                            $uri ='https://api.weixin.qq.com/cgi-bin/message/template/send';
           $uri = $uri.'?access_token='.$token;
          $data=  array(
            'touser'=>$openid,   //发给谁
            'template_id'=>$user['message_commission'],   //模板id

            "miniprogram"=>array(
                    "appid"=>"¥¥¥¥¥",
                    "pagepath"=>"pages/my/my?shop_id=".$shop_id."&enter=3"
             ),
            'topcolor'=>"#FF0000",   //颜色
            'miniprogram' => '',
            'data'=>array(

                'first'=>array(
                    'value'=>"恭喜您,¥¥¥¥¥",
                    'color'=>'#173177'
                ),
                'productType'=>array(
                    'value'=>"推荐会员昵称",
                    'color'=>'#3C3F41'

                ),
                'name'=>array(
                    'value'=>$shard['username'],
                    'color'=>'#173177'
                ),
                'accountType'=>array(
                    'value'=>'奖励',
                    'color'=>'#3C3F41'
                ),
                'account'=>array(
                    'value'=>$grain."米粒",
                    'color'=>'#173177'
                ),
                'time'=>array(
                    'value'=>date('Y-m-d H:i:s',time()),
                    'color'=>'#173177'
                ),

                'remark'=>array(
                    'value'=>'您可以在我的账户查看详情',
                    'color'=>'#173177'
                )
            )
        );
     $res_data = $this->getJson($uri,$data);

        $res_data = json_decode($res_data, true);

猜你喜欢

转载自blog.51cto.com/kangjunfei/2517896