批改狀態(tài):未批改
老師批語:
<?php namespace app\index\controller; use think\Controller; use think\facade\Cache; class Weixin extends Controller{ //構(gòu)造器 public function __construct(){ parent::__construct(); $this->model = model('Weixin'); //model模型中的Weixin類 } // 微信推送事件 public function index(){ // 校驗(yàn)數(shù)據(jù)來源 $valid = $this->model->valid(); //用$valid變量來接收model目錄中的valid()方法返回的數(shù)據(jù) if(!$valid){ exit('signature error'); //不通過,返回錯誤信息 } exit(input('get.echostr')); //通過,返回隨機(jī)字符串 } }
<?php namespace app\index\model; use think\Model; use think\facade\Cache; use think\Db; class Weixin extends Model{ // 簽名校驗(yàn) public function valid(){ $signature = input('get.signature'); $timestamp = input('get.timestamp'); $nonce = input('get.nonce'); $token = config('app.weixintoken'); //從app.php配置文件中拿到數(shù)據(jù) $tmpArr = array($timestamp,$nonce,$token); //用$tmpArr變量來接收數(shù)組中的數(shù)據(jù) sort($tmpArr, SORT_STRING); $str = implode($tmpArr); //將$tmpArr數(shù)組中的數(shù)據(jù)拼接來一個字符串 if(sha1($str) != $signature){ //判斷時將$str進(jìn)行sha1加密,$str不等于$signature時執(zhí)行false,否則返回true return false; //不通過返回false } return true; //通過返回true } //獲取access_token public function access_token($iscache = true){ $key = 'access_token'; if(!$iscache){ Cache::rm($key); } $data = Cache::get($key); //將拿到的數(shù)據(jù)存儲到$data變量中 if($data && $iscache){ //如果 $data 并且 $iscache有值 將返回當(dāng)前值 return $data; } $appid = config('app.appid'); //在app.php配置文件中設(shè)置值 $appsecret = config('app.appsecret'); //在app.php配置文件中設(shè)置值 //URL中必須配置公眾號里的appid 和 appsecret $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$appsecret; $res = http_get($url); $res = json_decode($res,true); if(!isset($res['access_token'])){ //檢測$res中沒有值則返回false,如果有值拿到下面語句中緩存 return false; } Cache::set($key,$res['access_token'],($res['expires_in']-500)); //緩存accseet_token, return $res['access_token']; } }
//微信token 'weixintoken' => 'p醫(yī)院醫(yī)院*', //開發(fā)者ID:APPID 'appid' => 'w醫(yī)院醫(yī)院醫(yī)院醫(yī)院醫(yī)院c', //開發(fā)者密碼 'appsecret' => 'e8醫(yī)院醫(yī)院*6醫(yī)院醫(yī)院醫(yī)院醫(yī)院*c醫(yī)院5醫(yī)院醫(yī)院醫(yī)院97醫(yī)院*',
function http_Post($url,$data){ $curl = curl_init(); curl_setopt($curl,CURLOPT_URL,trim($url)); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); //啟用時會發(fā)送一個常規(guī)的POST請求,為1或者為true if(!empty($data)){ $data = is_array($data)?json_encode($data):$data; curl_setopt($curl,CURLOPT_POST,1); curl_setopt($curl,CURLOPT_POSTFIELDS,$data);//需要要傳送的內(nèi)容 } curl_setopt($curl,CURLOPT_RETURNTRANSFER,1); $return_str = curl_exec($curl); curl_close($curl); return $return_str; } function http_Get($url){ $curl = curl_init(); curl_setopt($curl,CURLOPT_URL,trim($url)); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($curl,CURLOPT_HEADER,0); curl_setopt($curl,CURLOPT_CUSTOMREQUEST,'GET');//需要要傳送的內(nèi)容 curl_setopt($curl,CURLOPT_RETURNTRANSFER,1); $return_str = curl_exec($curl); curl_close($curl); return $return_str; }
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號