博主信息
博文 38
粉丝 0
评论 1
访问量 37094
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
微信公众号号开放之服务器配置—2018年6月14日
1
原创
1263人浏览过

这次作业难产了,原因在于我太自大了,在赶进度的过程中不重视作业和代码,导致卡在了微信公众号这里。我开始反省我是为了赶进度还是为了学知识,于是我根据在微信那里不懂不足的地方记下来,在重新看视频和翻看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举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
  • 1
    2018-03-16 00:39:40
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学