相關(guān)關(guān)鍵詞
關(guān)于我們
最新文章
- PHP中opcode緩存簡單用法分析
- thinkPHP控制器變量在模板中的顯示方法示例
- PHP move_uploaded_file() 函數(shù)(將上傳的文件移動(dòng)到新位置)
- dirname(__FILE__)的含義和應(yīng)用說明
- thinkPHP5框架實(shí)現(xiàn)分頁查詢功能的方法示例
- PHP中單雙號(hào)與變量
- PHP獲得當(dāng)日零點(diǎn)時(shí)間戳的方法分析
- Laravel ORM對(duì)Model::find方法進(jìn)行緩存示例詳解
- PHP讀寫文件高并發(fā)處理操作實(shí)例詳解
- 【CLI】利用Curl下載文件實(shí)時(shí)進(jìn)度條顯示的實(shí)現(xiàn)
基于thinkPHP3.2實(shí)現(xiàn)微信接入及查詢token值的方法
本文實(shí)例講述了基于thinkPHP3.2實(shí)現(xiàn)微信接入及查詢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'); //獲取微信服務(wù)器GET請求的4個(gè)參數(shù) $signature = I('signature'); $timestamp = I('timestamp'); $nonce = I('nonce'); $echostr = I('echostr'); if (! empty ( $echostr) && ! empty ( $signature ) && ! empty ($nonce )) { //定義一個(gè)數(shù)組,存儲(chǔ)其中3個(gè)參數(shù),分別是timestamp,nonce和token $tempArr = array($nonce,$timestamp,$token); //進(jìn)行排序 sort($tempArr,SORT_STRING); //將數(shù)組轉(zhuǎn)換成字符串 $tmpStr = implode($tempArr); //進(jìn)行sha1加密算法 $tmpStr = sha1($tmpStr); //判斷請求是否來自微信服務(wù)器,對(duì)比$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內(nèi)容 } $outopt = curl_exec($ch); curl_close($ch); $outopt = json_decode($outopt,true); return $outopt; } }