PHP实现发送模板消息(微信公众号版)

以下为开发步骤:

  1. 微信公众号为服务号且开通微信认证(其他类型账号不能发送)
  2. ip白名单设置你的服务器ip(用于获取access_token)
  3. 网页授权你的域名(用于获取用户的openid)
  4. 开通模板消息并在模板库中选用模板
  5. 获取openid
  6. 发送模板消息

ip白名单

在这里插入图片描述
在这里插入图片描述

网页授权

在这里插入图片描述
在这里插入图片描述

开通模板消息并选用模板

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

获取openid

[这是官方给出的文档]

  1. 用户同意授权,获取code
    接口地址:https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE#wechat_redirect
参数说明
appid公众号appid基础设置里有(必填)
redirect_uri重定向地址,用于接收code(必填)
response_type返回类型,请填写code(必填)
scope应用授权作用域,snsapi_base (不弹出授权页面,直接跳转,只能获取用户openid),snsapi_userinfo (弹出授权页面,可通过openid拿到昵称、性别、所在地。并且, 即使在未关注的情况下,只要用户授权,也能获取其信息 )(必填)
#wechat_redirect无论直接打开还是做页面302重定向时候,必须带此参数(必填)

完成参数填写后直接扔进你的自定义菜单栏里,点击跳转url

  1. 通过code换取网页授权access_token
    接口地址:https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code
        $code=$request->get("code"); //接收code,这里我用的laravel框架
        $url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=appid&secret=secret&code=".$code."&grant_type=authorization_code";
        $res=HttpUtils::curl($url, $params = false, $ispost = 0, $https = 1);//此方法为curl发送请求,可联系我要完整代码
        $res = (array)json_decode($res); // 返回结果为json,其中包含openid,access_token
参数说明
appid公众号appid(必填)
secret公众号secret基础配置里生成(必填)
code第一步获取的code(必填)
grant_type填写为authorization_code(必填)

正确返回的结果:

{ "access_token":"ACCESS_TOKEN",
"expires_in":7200,
"refresh_token":"REFRESH_TOKEN",
"openid":"OPENID",
"scope":"SCOPE" }

其中openid扔进你的数据库,发送模板消息的时候用

发送模板消息

在这里插入图片描述

  1. 获取access_token
    接口地址:https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=appid&secret=secret
参数说明
appid公众号appid(必填)
secret公众号secret(必填)
grant_type获取access_token填写client_credential(必填)
  1. 拼接模板消息
$data=[
            "touser"=>$openid, //对方的openid,前一步获取
            "template_id"=>"EVcUo-BP_A59s8sXjmYDZPEXtbaMpOCwVQguN4TUwHY", //模板id
            "miniprogram"=>["appid"=>"", //跳转小程序appid
                "pagepath"=>"pages/index/index"],//跳转小程序页面
            "data"=>[
                "first"=>[
                    "value"=> "你的账户即将到期,请及时缴费", //自定义参数
                    "color"=> '#173177'//自定义颜色
                ],
                "keyword1"=>[
                    "value"=> $account, //自定义参数
                    "color"=> '#173177'//自定义颜色
                ],
                "keyword2"=>[
                    "value"=> $time, //自定义参数
                    "color"=> '#173177'//自定义颜色
                ],
                "remark"=>[
                    "value"=> "如有疑问,请联系当地网点", //自定义参数
                    "color"=> '#173177'//自定义颜色
                ],
            ]
        ];
  1. 发送模板消息
        $url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=appid&secret=secret";  //此时再次请求access_token,与获取openid的接口不同!!!
        $access_token=json_decode(self::curl($url))->{"access_token"}; //我自己封装的curl类,可联系我获取
        $msgurl="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$access_token; // 发送模板消息接口 
        return json_decode(self::curl($msgurl,$params=json_encode($data),$ispost=1,$https=1));

总结

1.openid获取需要网页获取
2.接口地址严格按照官方所给出的地址填写,参数顺序不能错
3.发送模板消息时获取的access_token具有2小时的时效可丢进缓存中,不必每次发送都获取,每天只有两千次,模板消息发送次数为10万次,当然根据你公众号的关注人数来确定,人数超过10万肯定具有更高的次数
4.下篇文章将写小程序发送模板消息
5.常用的微信工具类,作者已封装,有兴趣的可以加群获取
6.作者自己写了个小程序BadBoy,有兴趣的可以搜索来玩玩,其中的代码可找作者索取
7.微信小程序交流群:895964328 php交流群:165728481

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
要在 PHP发送微信公众模板消息,首先需要在微信公众平台中创建一个模板消息并获取模板 ID。接下来,您需要在 PHP 中使用 cURL 库向微信 API 发送 POST 请求,以便将模板消息发送给用户。以下是示例代码: ```php $access_token = 'YOUR_ACCESS_TOKEN'; $template_id = 'YOUR_TEMPLATE_ID'; $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$access_token; $data = array( 'touser' => 'OPENID', 'template_id' => $template_id, 'data' => array( 'first' => array('value' => 'Hello, world!'), 'keyword1' => array('value' => 'Keyword 1'), 'keyword2' => array('value' => 'Keyword 2'), 'remark' => array('value' => 'This is a remark.') ) ); $data_string = json_encode($data); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json', 'Content-Length: ' . strlen($data_string) )); $result = curl_exec($ch); curl_close($ch); echo $result; ``` 上述代码中,`$access_token` 是您的公众号访问令牌,`$template_id` 是您创建的模板消息的 ID。您需要将 `OPENID` 替换为要接收模板消息的用户的 OpenID。`$data` 数组包含模板消息的详细信息,其中 `first`、`keyword1`、`keyword2` 和 `remark` 分别对应模板消息中的不同部分。最后,使用 cURL 库将 `$data` 数组作为 JSON 字符串发送微信 API,然后解析响应以查看是否成功。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值