您好,欢迎访问一九零五行业门户网

thinkPHP3.2实现微信接入及查询token值的方法

这篇文章主要介绍了基于thinkphp3.2实现微信接入及查询tooken值的方法,结合实例形式分析了thinkphp整合微信接口操作token值查询的具体实现步骤与相关操作技巧,需要的朋友可以参考下
本文实例讲述了基于thinkphp3.2实现微信接入及查询token值的方法。分享给大家供大家参考,具体如下:
1.在con.fig文件里面配置token,appid,appsecret值
2.控制器weixincontroller代码:
<?php/** * 微信父类控制器 * @author songle * */namespace weixin\controller;use think\controller;class weixincontroller extends controller { private $last_time=null; private $appid=null; private $appsecret=null; function __construct(){ parent::__construct(); $token=c('token'); $this->appid=c('appid'); $this->appsecret=c('appsecret'); //获取微信服务器get请求的4个参数 $signature = i('signature'); $timestamp = i('timestamp'); $nonce = i('nonce'); $echostr = i('echostr'); if (! empty ( $echostr) && ! empty ( $signature ) && ! empty ($nonce )) { //定义一个数组,存储其中3个参数,分别是timestamp,nonce和token $temparr = array($nonce,$timestamp,$token); //进行排序 sort($temparr,sort_string); //将数组转换成字符串 $tmpstr = implode($temparr); //进行sha1加密算法 $tmpstr = sha1($tmpstr); //判断请求是否来自微信服务器,对比$tmpstr和$signature if($tmpstr == $signature) { echo $echostr; } exit(); } } /** * 获取tooken值 */ public function gettooken(){ $this->last_time = 1448012924; $access_token = "填写上一次的token值"; //需要替换成自己的 if(time() > ($this->last_time + 7200)) { //get请求的地址 $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$this->appid}&secret={$this->appsecret}"; $access_token_arr = $this->https_request($url); $this->last_time = time(); return $access_token_arr['access_token']; } return $access_token; } //https请求(支持get和post) public function https_request($url,$data = null) { $ch = curl_init(); curl_setopt($ch,curlopt_url,$url); curl_setopt($ch,curlopt_returntransfer,1); if(!empty($data)) { curl_setopt($ch,curlopt_post,1); //模拟post curl_setopt($ch,curlopt_postfields,$data); //post内容 } $outopt = curl_exec($ch); curl_close($ch); $outopt = json_decode($outopt,true); return $outopt; }}
以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注!
相关推荐:
php使用token防止表单重复提交的方法,token表单
以上就是thinkphp3.2实现微信接入及查询token值的方法的详细内容。
其它类似信息

推荐信息