介绍
这篇文章主要介绍了小程序下获取access_token的案例,具有一定借鉴价值,需要的朋友可以参考下。希望大家阅读完这篇文章后大有收获。下面让小编带着大家一起了解一下。
& lt; button bindtap=皌oken"在获取token
//,/令牌/token.js页面 页面({ ,,数据:{ ,,}, 令牌才能:函数(){ ,,,wx.request ({ ,,,,,url:, & # 39; xxxx # 39; ,,,,,数据:{}, ,,,,,成功:函数(res) { ,,,,,,,console.log (res.data); ,,,,,,,//这里我是用的新浪云的sae,所以存在读写权限问题,打印出来的信息会存在告警 ,,,,,} ,,,}); ,,} })
& lt; php 退出();?比; {“access_token":““,“expire_time": 0}
& lt; php ?//才能验证 function 才能;getToken () { ,,,timestamp 美元;=,$ _GET [“timestamp"]; ,,,nonce 美元;=$ _GET [“nonce"]; ,,,令牌=美元“hello hqtoken"; ,,,美元签名=$ _GET [“signature"]; ,,,美元数组=数组(时间戳,目前美元,美元令牌); ,,,(数组); ,,,$ tmpstr=内爆(““,数组美元); ,,,美元tmpstr=sha1 (tmpstr美元); ,,,如果美元($ tmpstr ==,签名){ ,,,,,echo $ _GET [“echostr"]; ,,,,,退出; ,,,} ,,}//才能获取AccessToken function 才能;getAccessToken () { ,,,$ appid=澳愕腶ppid"; ,,,appsecret 美元;=,“你的appsecret"; ,,,//文件存储 ,,,$ data=https://www.yisu.com/zixun/json_decode (get_php_file (access_token.php ")); 如果($数据-> expire_time <时间()){ url=" https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="。appid美元。“秘密=" $ appsecret;。 $ res=json_decode (httpGet (url)美元); 美元access_token=$ res -> access_token; 如果(access_token美元){ $ data () -> expire_time=时间+ 7000; 数据-> access_token=access_token美元; set_php_file (“access_token.php json_encode(元数据)); } 其他}{ access_token=美元数据-> access_token; } 返回access_token美元; }//公共旋度的方法 函数httpGet ($ url) { 旋度=美元curl_init (); curl_setopt ($ curl CURLOPT_URL $ url); curl_setopt(美元curl CURLOPT_RETURNTRANSFER 1);//如果用的是https, ssl安全验证 curl_setopt ($ curl CURLOPT_SSL_VERIFYPEER假); curl_setopt ($ curl CURLOPT_SSL_VERIFYHOST假); (res=curl_exec美元旋度); curl_close($旋度); 返回res美元; } 函数get_php_file ($ filename) { 返回修剪(substr (file_get_contents(文件名美元),15)); } 函数set_php_file(文件名、内容美元){ 美元$ fp=fopen(文件名,“w”); 写入文件(fp美元," <?php退出();?>”。$内容); 文件关闭(fp); } 回声getAccessToken (); ?>
感谢你能够认真阅读完这篇文章,希望小编分享小程序下获取access_token的案例内容对大家有帮助,同时也希望大家多多支持,关注行业资讯频道,遇到问题就找,详细的解决方法等着你来学习!