微信发送模版消息

    我们需要将一些行为的进展消息推送给用户。除了短信,发送微信模板消息也是不错的选择。模板消息免费、精准到达、而且可以引导用户回到网站上来。但它有两个前提条件。1个是认证的服务号,你才能选择模板。2个是被推送的用户必须关注了你的公众号,而且你也拿到了他的openid

先在模板库中找到自己的想要的模板,添加到“我的模板”中。

展开详情,我们可以看到示例。

接下来用PHP代码发送一次:

class Wechat
{

    //首先获取获取access_token
    public static function get_access_token(){
        $ch = curl_init(); //初始化一个CURL对象
        $appId = "xxxxxxxxxxxxxxxxxx"; //微信appid
        $appSecret = "xxxxxxxxxxxxxxxxxx"; //微信appsecret
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appId."&secret=".$appSecret;
        $ch = curl_init();//初始化curl
        curl_setopt($ch, CURLOPT_URL,$url); //要访问的地址
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//跳过证书验证
        $data = json_decode(curl_exec($ch));
        if($data->access_token){
            $token_file = fopen("token.txt","w") or die("Unable to open file!");//打开token.txt文件,没有会新建
            fwrite($token_file,$data->access_token);//重写tken.txt全部内容
            fclose($token_file);//关闭文件流
        }else{
            echo $data->errmsg;
        }
        curl_close($ch);
    }
    
    //读取access_token的方法
    public static function read_token(){
        $token_file = fopen("token.txt", "r") or die("Unable to open file!");
        $rs = fgets($token_file);
        fclose($token_file);
        return $rs;
    }
    
    //发送模版消息
    public  function send(){
        $this->build_access_token();
        $ACCESS_TOKEN=$this->read_token();
        $data=array(
            'touser'=>"olfsB1VwJLKYsGbss90z7J-3baE4", //要发送给粉丝的openid
            'template_id'=>"0C3WQsssss8pzMesCyrU5_8pm2Abmags7DydiTaOdUM",//改成自己的模板id,在微信后台模板消息里查看
            'url'=>"http://www.xxxxxxx.com/weixin/", //自己网站链接url
            'data'=>array(
                'first'=>array('value'=>"XXX,你好",'color'=>"#fc0101"),
                'keyword1'=>array('value'=>"XXX门店",'color'=>"#173177"),
                'keyword2'=>array('value'=>"2018-12-12",'color'=>"#173177"),
                'keyword3'=>array('value'=>"美容",'color'=>"#173177"),
                'remark'=>array('value'=>"欢迎光临。",'color'=>"#173177"),
            )
        );

        $json_data=json_encode($data);//转化成json数组让微信可以接收
        $url="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$ACCESS_TOKEN;//模板消息请求URL
        $res=$this->https_request($url,urldecode($json_data));//请求开始
        $res=json_decode($res,true);
        if($res['errcode']==0 && $res['errcode']=="ok"){
            echo "发送成功!";
        }
    }

     
    //curl请求函数,微信都是通过该函数请求
    function https_request($url,$data = null){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if (!empty($data)){
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }
}

猜你喜欢

转载自blog.csdn.net/fuhanghang/article/details/83384888