使用PHP編寫發紅包程序
746
2023-12-10
本文實例講述了基于thinkPHP3.2實現微信接入及查詢token值的方法。分享給大家供大家參考,具體如下:
1.在con.fig文件里面配置TOKEN,APPID,APPSECRET值
2.控制器WeixinController代碼:
<?php /** * 微信父類控制器 * @author Songle * */ namespace WeixinController; use ThinkController; 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程序設計有所幫助。
#免責聲明#
本站[綠夏技術導航]提供的一切軟件、教程和內容信息僅限用于學習和研究目的;不得將上述內容用于商業或者非法用途,否則,一切后果請用戶自負。本站信息來自網絡收集整理,版權爭議與本站無關。您必須在下載后的24個小時之內,從您的電腦或手機中徹底刪除上述內容。如果您喜歡該程序或內容,請支持正版,購買注冊,得到更好的正版服務。我們非常重視版權問題,如有侵權請郵件[admin@lxwl520.com]與我們聯系進行刪除處理。敬請諒解!