欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

微信公众平台开发-模板消息

程序员文章站 2022-06-12 20:25:37
...

Github完整源码:https://github.com/xiaoming000/wechat.git

官方文档地址:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1433751277

正常的模板消息使用步骤是选择所处的行业(可选择2个),然后申请模板(同时最多25个);

但是我们用的测试账号,所以直接在测试账号中按要求设置模板消息,然后进行模板消息发送!

 

<?php

require "public.php";


class Templates{
    // http请求方式: POST
    // https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN
    public function send_template($openid, $name){
        $arr = array(
            "touser"=>$openid,
            "template_id"=>"7D5ybniJpsoxq8NXi-09MWPm7qhNBrCYhyoIXrMiPts",
            "url"=>"http://weixin.qq.com/download",
            "data"=>array(
                'name'=>array('value'=>$name, 'color'=>"#173177")
            )
        );
        $fun = new PublicFunction();
        $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$fun->getWxAccessToken();
        $res = $fun -> http_curl($url, 'post', 'json', json_encode($arr));
        var_dump($res);
    }
}

$temp = new Templates();
$openid = "oyfLo1Uh9dhVXb6uEYkYM1bmdqt0";
$name = "xiaoxiaoming";
$temp -> send_template($openid, $name);
// 这里关注的模板消息回复只需要的index.php的被关注事件中调用这个函数就可以!