微信小程序服务通知

项目中用到了小程序的服务消息通知,通知订单状态信息,下边就是整理的一下代码,放到项目中,把项目的小程序appid和小程序的secret写进去,直接运行即可

提前申请好小程序服务信息通知短信模板,代码需要用到模板id

public static function curl(url, params = false, ispost = 0, https = 0)

{

$httpInfo = array();

$ch = curl_init();

curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);

curl_setopt( $ch, CURLOPT_HTTPHEADER, array(

'Content-Type: application/json; charset=utf-8'

)

);

curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36');

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);

curl_setopt($ch, CURLOPT_TIMEOUT, 30);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

if ($https) {

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // 对认证证书来源的检查

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); // 从证书中检查SSL加密算法是否存在

}

if ($ispost) {

curl_setopt($ch, CURLOPT_POST, true);

curl_setopt(ch, CURLOPT_POSTFIELDS, params);

curl_setopt(ch, CURLOPT_URL, url);

} else {

if ($params) {

if (is_array($params)) {

params = http_build_query(params);

}

curl_setopt(ch, CURLOPT_URL, url . '?' . $params);

} else {

curl_setopt(ch, CURLOPT_URL, url);

}

}

response = curl_exec(ch);

if ($response === FALSE) {

return false;

}

httpCode = curl_getinfo(ch, CURLINFO_HTTP_CODE);

httpInfo = array_merge(httpInfo, curl_getinfo($ch));

curl_close($ch);

return $response;

}

public static function access_token(){

$appid="--------"; //小程序appid

$secret="-------"; //小程序secret

Url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential\&appid=". appid."&secret=".$secret; //微信给出的获取access_token的接口

$access_token=Cache::get("access_token"); //查询缓存中是否已存在access_token

if($access_token==""){

access_token=json_decode(self::curl(Url))->{"access_token"}; //访问接口获取access_token

}

return $access_token;

}

public static function SendMsg(data,access_token){

MsgUrl="https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=".access_token; //微信官方接口,需要拼接access_token

return json_decode(self::curl(MsgUrl,params=json_encode(data),ispost=1,$https=1)); //访问接口,返回参数

}

public function test(Request $request){

$openid='------------';//接收信息用户的openID,如果存在数据库中,可在数据库中查找

access_token=this->access_token();

$data=[

"touser"=>$openid, //接收用户的openid

"template_id"=>"---------", //短信模板id

"page"=>"pages/index/index",//点击模板消息跳转至小程序的页面

"data"=>[

//data数组下的键值一定要和申请短信模板一直,否则会报错

"thing2"=>[

"value"=> "您邀请的好友'E-June'订单已完成", //自定义参数

],

"amount1"=>[

"value"=> "3.8元",//自定义参数

],

"thing3"=>[

"value"=> "水果熟了 推荐有奖",//自定义参数

],

"time4"=>[

"value"=> "2021年05月26日 11:15",//自定义参数

],

"thing5"=>[

"value"=> "客服审核通过自动增加收益",//自定义参数

],

]

];

res=this->SendMsg(data,access_token); //返回结果

var_dump($res);die;

}

}

执行方法test即可发送成功,前提是小程序要允许小程序的服务通知才行

测试时执行结果是0,并且ok就发送成功,其他返回代码可以在微信公众平台查看,这里就不解释了
© 著作权归作者所有,转载或内容合作请联系作者

喜欢的朋友记得点赞、收藏、关注哦!!!

相关推荐
Arvin6271 天前
Jenkins 任务执行完成后会kill掉的衍生进程
运维·servlet·jenkins
yaaakaaang1 天前
五、原型模式
java·原型模式
chh5631 天前
从零开始学C++--类和对象
java·开发语言·c++·学习·算法
雄哥0071 天前
linux redis升级⼿册-源码部署版
linux·运维·redis
總鑽風1 天前
数据一致性springcloud+rabbitmq+mysql+redis
mysql·spring cloud·rabbitmq
水云桐程序员1 天前
电子自动化技术(EDA技术)FPGA概述
运维·fpga开发·自动化
用户9751470751361 天前
Tomcat安装及配置教程(保姆级)
java
爱浦路 IPLOOK1 天前
选对UPF网元供应商,解锁5G-A时代行业赋能新可能
运维·网络·数据库
列星随旋1 天前
基于 Redis + Lua,实现“多维度原子限流”(令牌桶 + 滑动窗口)
java·redis·lua
cyber_两只龙宝1 天前
【Nginx】Nginx反向代理之实现http的反向代理
linux·运维·nginx·http·云原生·反向代理