博主信息
1
博文
38
粉丝
0
评论
1
访问量
15923
积分:0
P豆:401

微信公众号号开放之服务器配置—2018年6月14日

2018年06月14日 01:09:08阅读数:651博客 / 1/ 作业

这次作业难产了,原因在于我太自大了,在赶进度的过程中不重视作业和代码,导致卡在了微信公众号这里。我开始反省我是为了赶进度还是为了学知识,于是我根据在微信那里不懂不足的地方记下来,在重新看视频和翻看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);
	}

运行实例 »

点击 "运行实例" 按钮查看在线实例


批改状态:合格

老师批语:

版权申明:本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!

全部评论

文明上网理性发言,请遵守新闻评论服务协议

条评论