这次作业难产了,原因在于我太自大了,在赶进度的过程中不重视作业和代码,导致卡在了微信公众号这里。我开始反省我是为了赶进度还是为了学知识,于是我根据在微信那里不懂不足的地方记下来,在重新看视频和翻看tp5的手册,终于终于搞了个7788,也算是写出来了。
controller/Weixin.php
实例
<?php namespace app\index\controller; use think\Controller; class Weixin extends Controller { public function __construct() { parent::__construct(); //model/weixin $this->model = model('Weixin'); } //验证 public function index() { $vail = $this->model->vail(); if (!$vail) { exit('signature error'); } exit(input('get.echostr')); } } ?>
运行实例 »
model/Weixin.php
实例
<?php namespace app\index\model; use think\Model; use think\facade\Cache; class Weixin extends Model { public function Vail() { $signature = input('get.signature'); $timestamp = input('get.timestamp'); $nonce = input('get.nonce'); $token = config('app.token'); // file_put_contents('D://data.txt','signature='.$signature.'timestamp='.$timestamp.'nonce='.$nonce.'echostr='.$echostr); // exit(); $tmpArr = array($timestamp,$nonce,$token); sort($tmpArr, SORT_STRING); $tmpStr = implode( $tmpArr ); if(sha1($tmpStr)!= $signature ){ return false; } return true; } //缓存的开关 public function get_access_token($iscache = true) { $key = 'access_token'; if(!$iscache) { Cache::rm($key); } $access_token = Cache::get($key); if($access_token && $iscache) { return $access_token; } // $appID = config('app.appID'); // $appsecret = config('app.appsecret'); $appID = config('app.weixin_id'); $appsecret = config('app.weixin_secret'); $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); Cache::set($key,$res['access_token'],$res['expires_in']-600); return $res['access_token']; // dump($res); }
运行实例 »
点击 "运行实例" 按钮查看在线实例