2025年微信小程序推送公众号模板消息

微信小程序推送公众号模板消息1 准备工作 微信公众号 AppId 和 APPSecret 必须认证 微信小程序 AppId 和 APPSecret 必须认证 微信开放者平台 小程序和公众号必须绑定同一个开放者平台 必须认证 1 小程序关联公众号 1 1 路径 小程序后台 设置 关注公众号 2

大家好,我是讯享网,很高兴认识大家。

1、准备工作

微信公众号:AppId和APPSecret(必须认证)
微信小程序:AppId和APPSecret(必须认证)
微信开放者平台(小程序和公众号必须绑定同一个开放者平台,必须认证)

1. 小程序关联公众号:

1.1 路径:小程序后台——设置——关注公众号


讯享网

2. 获取用户是否关注公众号标识

        2.1 路径:小程序开发设置——业务域名,添加公众号设置的 网页授权域名 才可以访问

               2.1.1 小程序设置跳转路径 获取code:URL必须是上面设置好的域名下的路径

APPID:你的appid值 redirect_uri:回调地址,注意https://www.xxxxxxx.com 小程序的业务域名必须配置 <web-view src="https://open.weixin..com/connect/oauth2/authorize?appid=APPID&redirect_uri=https://www.xxxxxxx.com/mainapp.php/Login/actionIndex&response_type=code&scope=snsapi_userinfo#wechat_redirect"></web-view>

讯享网

                2.1.2 接收code值获取用户openid值

讯享网 / * Info: //获取openid * @param string $code code值 */ public function actionIndex(){ $code = $_GET["code"];//获取前端给的code值 $appid = '微信公众号appid';//微信公众号appid $appsecret = '微信公众号appsecret';//微信公众号appsecret // 公众号 $url = "https://api.weixin..com/sns/oauth2/access_token?appid=".$appid."&secret=".$appsecret."&code=".$code."&grant_type=authorization_code"; $token = file_get_contents($url); $token = json_decode($token,true); $user_url = "https://api.weixin..com/sns/userinfo?access_token=".$token['access_token']."&openid=".$token["openid"]."&lang=zh_CN"; $userinfo = file_get_contents($user_url); $arr = json_decode($userinfo,true); $openid = $arr['openid']; $unionid = $arr['unionid'];/*该字段必须在微信开发平台(https://open.weixin..com/)进行关联*/ }

                2.1.3 接收code值获取用户openid值

 / * 发送模板消息 */ public function send_notice(){ $appid = '微信公众号appid';//微信公众号appid $appsecret = '微信公众号appsecret';//微信公众号appsecret $access_token_url = 'https://api.weixin..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..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' => '用户openid', //用户openid 'template_id' => "在公众号下配置的模板id", //在公众号下配置的模板id 'url' => "http://baidu.com", //点击模板消息会跳转的链接 //如果想要跳转微信小程序,就把上面这个url这一行注释掉,用下面这个`miniprogram` //'miniprogram' => [ //'appid' => '这里填写要跳转的小程序appid', //'pagepath' => 'pages/index/index?order_id=205', //这里填写小程序路径,可以拼接参数 // ], '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; } / * @param $url * @param array $data * @return mixed * 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; } 

小讯
上一篇 2025-02-18 21:48
下一篇 2025-02-15 22:45

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/97468.html