[入门]PHP之公众号发送模板消息

由于是入门,我们使用微信公众平台的测试号即可


微信公众平台接口测试帐号申请

  • 进入 https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login 扫码登录微信即可注册完成
  • 注册完成之后会获得appID和appsecret,留着备用
  • 然后扫描测试号二维码关注一下,之后测试就会通过这个公众号给你发送消息模板,扫描关注之后会出现一个openid,也留着备用
  • 最后新增一个消息模板,获得到一个模板id,也就是template_id,也留着备用

PHP后端代码

  • 这里我使用的是tp5框架,你也可以根据你的实际使用来修改,道理是一样的
  • 下面的代码是放在Index控制器里,所以待会直接在浏览器访问 http://localhost:8080/news/public/index.php/index/index/do_send 即可发送消息模板,这里的本地端口是8080,默认是80,根据自己的设置修改就好了,如果你用的不是tp5,那么直接直接复制下面三个函数也是可以用的
<?php
namespace app\index\controller;
use think\Db;
use think\Controller;
class Index extends Controller
{
    //发送模板消息
    public function do_send(){
        echo "正在发送模板消息-------------";//便于debug,实际使用可以删除
        $appid = '填写前面获取的appid';
        $appsecret = '填写前面获取的appsecret';
        $access_token_url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$appsecret;
        //获取access_token
        $json_token=$this->curl_post($access_token_url);
        $access_token1=json_decode($json_token,true);
        $access_token2=$access_token1['access_token'];
        //模板消息
        $json_template = $this->json_tempalte();
        $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$access_token2;
        $res = $this->curl_post($url,urldecode($json_template));
        $res = json_decode($res,true);
        if ($res['errcode']==0){
            return '发送成功';
        }else{
            return '发送失败';
        }
    }

    //将模板消息json格式化
    public function json_tempalte(){
        //模板消息
        $template=[
            'touser'      => 'oZjyn6A0EfbPERwP1CO4FA3dUW9Y',  //前面获取的openid
            'template_id' => "wvwS7xiECMoxldhHjkaiXLpvsmnGQ5MeSf-kgpS-hvM", //前面新增的模板id
            'url'		  => "https://www.coldmoon.cn/", //点击模板消息会跳转的链接
            'topcolor'    => "#7B68EE",
            'data'=>array(
                //这里在测试号是没法显示的,但是在正式的模板是这样的数据
                'first'=>array('value'=>urlencode("测试"),'color'=>"#FF0000"),
                'keyword1'=>array('value'=>urlencode('测试'),'color'=>'#FF0000'),  //keyword需要与配置的模板消息对应
                'keyword2'=>array('value'=>urlencode(date("Y-m-d H:i:s")),'color'=>'#FF0000'),
                'keyword3'=>array('value'=>urlencode('测试'),'color'=>'#FF0000'),
                'keyword4'=>array('value'=>urlencode('测试'),'color'=>'#FF0000'),
                'remark' =>array('value'=>urlencode('测试'),'color'=>'#FF0000'), )
        ];
        $json_template=json_encode($template);
        return $json_template;
    }

    //curl请求
    function curl_post($url , $data=array()){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        // POST数据
        curl_setopt($ch, CURLOPT_POST, 1);
        // 把post的变量加上
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }
}
  • 运行成功会如图显示
  • 然后微信公众号就可以看到了,我这里刷新了好几次,所以会发送多条

  • 最后放上官方开发文档的链接
  • https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Get_access_token.html
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值