微信自动发消息,微信小程序完整开发小程序模板消息,定时发送,附前台后台源码数据库参考
首先第一步,获取access_token这个access_token需要去微信公众平台去获取由于access_tokem是有过期时间的也就是2个小时,所以咱们需要新建一个数据库来存储这些access_token判断是否过期zhe
这是数据库结构接下来我们需要写一下后端代码publicfunctiongetaccess_token(){$appid='';$appsecret='';//这两个可以从微信公众平台获取}下一步贴一下代码,基本粘贴过去就能用的,换上自己的表跟字段publicfunctiongetaccess_token(){$appid='';$appselect='';//这两个可以从微信公众平台获取$sql=Db::table('xdk_token')-where(['appid'=$appid,'appselect'=$appselect])-find();$now=time();//获取当前时间戳if($now-$sql['apptime']7000){$token=$sql['token'];return$token;}else{$token_access="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appselect";//执行请求file_get_contents$tokens=file_get_contents($token_access);//JSON转换$tokening=json_decode($tokens);$token=$tokening-access_token;//获取Token字段值$data=['token'=$token,'apptime'=$now,'appid'=$appid,'appselect'=$appselect];Db::table('xdk_token')-insert($data);return$token;}}我这里是用的TP5框架写的,大家可以用在自己的写法写
这是数据库结构接下来我们需要写一下后端代码publicfunctiongetaccess_token(){$appid='';$appsecret='';//这两个可以从微信公众平台获取}下一步贴一下代码,基本粘贴过去就能用的,换上自己的表跟字段publicfunctiongetaccess_token(){$appid='';$appselect='';//这两个可以从微信公众平台获取$sql=Db::table('xdk_token')-where(['appid'=$appid,'appselect'=$appselect])-find();$now=time();//获取当前时间戳if($now-$sql['apptime']7000){$token=$sql['token'];return$token;}else{$token_access="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appselect";//执行请求file_get_contents$tokens=file_get_contents($token_access);//JSON转换$tokening=json_decode($tokens);$token=$tokening-access_token;//获取Token字段值$data=['token'=$token,'apptime'=$now,'appid'=$appid,'appselect'=$appselect];Db::table('xdk_token')-insert($data);return$token;}}我这里是用的TP5框架写的,大家可以用在自己的写法写声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。

