基于thinkPHP3.2实现微信接入及查询token值的方法
2017-04-18 08:56
981 查看
本文实例讲述了基于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; } }
更多关于thinkPHP相关内容感兴趣的读者可查看本站专题:《PHP微信开发技巧汇总》、《ThinkPHP入门教程》、《thinkPHP模板操作技巧总结》、《ThinkPHP常用方法总结》、《codeigniter入门教程》、《CI(CodeIgniter)框架进阶教程》、《Zend FrameWork框架入门教程》及《PHP模板技术总结》。
希望本文所述对大家基于ThinkPHP框架的PHP程序设计有所帮助。
您可能感兴趣的文章:
相关文章推荐
- 微信开发1.基于Thinkphp3.2网站实现微信接入以及查询tooken值
- Thinkphp使用mongodb数据库实现多条件查询方法
- 记录 “Thinkphp 3.2及以上版本实现支付宝担保交易、即时到账接口类、函数和使用方法”
- thinkPHP实现多字段模糊匹配查询的方法
- 基于thinkPHP实现的微信自定义分享功能示例
- jQuery基于xml格式数据实现模糊查询及分页功能的方法
- 数据库分表和分库的原理及基于thinkPHP的实现方法
- thinkphp3.2微信开发者模式 绑定url token 您的服务器没有正确相应token验证
- thinkPHP实现多字段模糊匹配查询的方法
- 基于thinkPHP框架实现留言板的方法
- spring security采用基于简单加密 token 的方法实现的remember me功能
- Thinkphp使用mongodb数据库实现多条件查询方法
- 基于微信地理位置的附近商家距离坐标数据查询方法
- 一个基于thinkphp 的 easyui datagrid 组件查询功能的实现
- php mysql procedure实现获取多个结果集的方法【基于thinkPHP】
- Thinkphp 3.2及以上版本实现支付宝担保交易、即时到账接口类、函数和使用方法
- thinkPHP3.2简单实现文件上传的方法
- thinkPHP3.2简单实现文件上传的方法
- 微信环境中定位获取城市名称(基于thinkphp3.2)
- thinkPHP简单实现多个子查询语句的方法