前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >公众号开发之发送模板信息

公众号开发之发送模板信息

作者头像
桑先生
发布2019-12-17 10:19:15
7280
发布2019-12-17 10:19:15
举报
文章被收录于专栏:桑先生的专栏桑先生的专栏

前阵子小程序项目中因为需要及时通知用户,就打算对接小程序的模板通知.可是说是小程序在明年的一月份就不支持了.所以就放下了.

今天有需要在公众号中发送模板消息.也是直接看了下文档直接来对接起来.也是很顺利.

首先,需要去公众号后台申请模板.

在微信公众平台-功能-模板消息里面申请.选择自己需要选择的分类.保存下模板id.

这里自己也是简单封装下.为以后需要作准备.这里简单分享下.

需要注意的是这里的Accesstoken  你需要根据根据自己的情况来保存一下.因为这个东西每天只有2000次的获取次数.保存到缓存或者数据库都可以.这里做最简单的分享

代码语言:javascript
复制
 /**
     公众号模板消息
     * $openid   需要发送用户的openid
     * $name 用户名字
     * $mobile 联系方式
     * $time 派单时间
     * $content 维修内容
     */
    public function templateInfo($openid,$name,$mobile,$time,$content)
    {
        $ACCESS_TOKEN = $this->getAccessToken();//通过微信获取access_token接口 获取的token
       // $openid = '';//用户openid
        $template_id = '';//配置的模板id
        $url = '';//点击模板消息跳转的链接
        $template = array(
            'touser' => $openid,
            'template_id' => $template_id,
            'url' => $url,
            'data' => array(
                'first' => array('value' => '您好,您有新的维修任务!', 'color' => "#173177"),
                'keyword1' => array('value' => $name, 'color' => '#173177'),
                'keyword2' => array('value' => $mobile, 'color' => '#173177'),
                'keyword3' => array('value' => $time, 'color' => '#173177'),
                'keyword4' => array('value' => $content, 'color' => '#173177'),),
                'remark' => array('value' => '请及时登录公众号接单确认!', 'color' => "#173177")
        );

        $send_template_url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=' . $ACCESS_TOKEN;

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $send_template_url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($template));
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        $res = curl_exec($ch);
        curl_close($ch);
        return $res;
    }
    /**
     * 获取access_token
     */
    public function getAccessToken()
    {

        $appId = db('config')->where(['name' => 'appid'])->value('value');
        $appSecret = db('config')->where(['name' => 'appsearct'])->value('value');
        $tokenUrl="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appId."&secret=".$appSecret;
        $getArr=array();
        $tokenArr=json_decode($this->send_post($tokenUrl,$getArr,"GET"));
        $access_token=$tokenArr->access_token;
        return $access_token;
    }

    /**
     * 请求方法
     */
    protected function send_post($url, $post_data,$method='POST')
    {

        $postdata = http_build_query($post_data);

        $options = array(

            'http' => array(

                'method' => $method, //or GET

                'header' => 'Content-type:application/x-www-form-urlencoded',

                'content' => $postdata,

                'timeout' => 15 * 60 // 超时时间(单位:s)

            )

        );
        $context = stream_context_create($options);

        $result = file_get_contents($url, false, $context);

        return $result;
    }

用的时候直接调用templateInfo 这个方法传入对应的参数就行. ?

效果如图下所示

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云开发 CloudBase
云开发(Tencent CloudBase,TCB)是腾讯云提供的云原生一体化开发环境和工具平台,为200万+企业和开发者提供高可用、自动弹性扩缩的后端云服务,可用于云端一体化开发多种端应用(小程序、公众号、Web 应用等),避免了应用开发过程中繁琐的服务器搭建及运维,开发者可以专注于业务逻辑的实现,开发门槛更低,效率更高。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档