ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
``` <?php $ACCESS_TOKEN = "";//ACCESS_TOKEN //openid数组 $touser = [ 'ovMzR1HD6LOcbcWLw-Xn9eKOyG1U' ]; //模板消息请求URL $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $ACCESS_TOKEN; //遍历发送微信消息 foreach ($touser as $value) { $data = getDataArray($value); $json_data = json_encode($data);//转化成json数组让微信可以接收 $res = https_request($url, urldecode($json_data));//请求开始 $res = json_decode($res, true); if ($res['errcode'] == 0 && $res['errcode'] == "ok") { echo "发送成功!<br/>"; }else { echo "发送失败!<br/>"; } } //获取发送数据数组 function getDataArray($value) { $data = array( 'touser' => $value, //要发送给用户的openid 'template_id' => "kKPT5I-wyENb81yHtg8WazchUFjsqtyQ1Rlu62lGaQw",//改成自己的模板id,在微信后台模板消息里查看 'url' => "http://ui-china.cn", //自己网站链接url 'data' => array( 'first' => array( 'value' => "标题名称", 'color' => "#000" ), 'keyword1' => array( 'value' => "员工姓名", 'color' => "#f00" ), 'keyword2' => array( 'value' => "2019-1-6", 'color' => "#173177" ), 'keyword3' => array( 'value' => "地点", 'color' => "#3d3d3d" ), 'keyword4' => array( 'value' => "类型", 'color' => "#3d3d3d" ), 'keyword5' => array( 'value' => "备注", 'color' => "#3d3d3d" ), 'remark' => array( 'value' => "1.备注详情\n2.备注详情>>>", 'color' => "#3d3d3d" ), ) ); return $data; } //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; } ?> ``` ========================================================== 获取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 2.通过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(必填) 2.拼接模板消息 ``` $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'//自定义颜色 ], ] ]; ``` 3.发送模板消息 $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"}; $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万肯定具有更高的次数 =========================================================== ``` <?php $ACCESS_TOKEN="这里写改成自己的ACCESS_TOKEN";// $touser = ['ouD7BuHpIKRXPIz7pdrwI9IwDRCU','ouD7BuI36wSUZgteyiydmDrldQLU','ouD7BuLejq7R4Vbuyh41bH778cg0'];//openid数组 $data=array( 'touser'=>$touser[0], //要发送给用户的openid 'template_id'=>"mfopDNUlvoBGGsPLB-d_nrfL8Je92xnTq5vk5ZBxL-w",//改成自己的模板id,在微信后台模板消息里查看 'url'=>"http://mp.weixin.qq.com/s/8UWPqHVa8PReWZp-No0ebA", //自己网站链接url 'data'=>array( 'first'=>array( 'value'=>"亲爱的同学,您有考试提醒,请查阅。", 'color'=>"#000" ), 'keyword1'=>array( 'value'=>"2017下半年教师资格证面试", 'color'=>"#f00" ), 'keyword2'=>array( 'value'=>"2018-1-6", 'color'=>"#173177" ), 'keyword3'=>array( 'value'=>"请看您的准考证", 'color'=>"#3d3d3d" ), 'keyword4'=>array( 'value'=>"教师资格证试讲", 'color'=>"#3d3d3d" ), 'keyword5'=>array( 'value'=>"答辩,选题,结构化", 'color'=>"#3d3d3d" ), 'remark'=>array( 'value'=>"\n现在是打印准考证时间,请您在考试前打印准考证,戳进来可以查看详情>>>", 'color'=>"#3d3d3d" ), ) ); $json_data=json_encode($data);//转化成json数组让微信可以接收 $url="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$ACCESS_TOKEN;//模板消息请求URL $res=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; } ?> ```