博主信息
博文 64
粉丝 2
评论 3
访问量 89498
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
微信公众号号开放之服务器配置—2018年6月2日16:09:58
清雨的博客
原创
1476人浏览过

因无认证公众号,过程采用微信公众号测试号进行,服务器采用阿里云服务器。

框架为thinkPHP 5.1.4

因在讲解服务器配置时有事没有听课,故而两天时间看视频研究代码。;来进行调试代码,在整个过程中,非常的崎岖,

问题一、因一直在本地环境进行测试,没有区分过大小写,在本地中测试没有问题,上传在linux服务多方报错,并查找原因,最后发现,在控制器文件名及模式的文件名全部小写。导致一直无法访问。

问题二、因用多个编辑器打开,导致文件被家务BOOT,在打开app_debug  提示

控制器Namespace declaration statement has to be the very first statement in the script  

并在多次查看文件都是在第一行,最后更换字符集报错解决。

附代码

controller/Weixin.php

实例

<?php
namespace app\index\controller;
use think\Collection;
use think\facade\Cache;

class Weixin extends Collection
{
    public function __construct(){
        parent::__construct();
        $this->model = model('Weixin');
    }
    // 微信推送事件
    public function index(){
        // 校验数据来源
        $valid = $this->model->valid();
        if(!$valid){
            exit('signature error');
        }   
        exit(input('get.echostr'));
    }
}

运行实例 »

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

model/Weixin.php

实例

<?php
namespace app\index\model;
use think\Model;
use think\facade\Cache;
use think\Db;

class Weixin extends Model{
    // 签名校验
    public function valid(){
        $signature = input('get.signature');
        $timestamp = input('get.timestamp');
        $nonce     = input('get.nonce');
        $token     = config('app.token');
        $tmpArr    = array($timestamp,$nonce,$token);
        sort($tmpArr, SORT_STRING);
        $str       = implode($tmpArr);
        if(sha1($str) != $signature){
            return false;
        }
        return true;
    }
	public function access_token($iscache = true){
		$key       = 'access_token';
        if(!$iscache){
            Cache::rm($key);
        }
		$data      = Cache::get($key);
		if($data && $iscache){
			return $data;
		}
		$appid     = config('app.appid');
    	$appsecret = config('app.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'])){
    		return false;
    	}
    	Cache::set($key,$res['access_token'],($res['expires_in']-100));
    	return $res['access_token'];
	}
}

运行实例 »

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

感受:

在整个写的过程中可以说是很崎岖,因为我在之前也看过很多的教程来讲微信开发,但是始终为一头雾水,原因:所讲视频内容比较早,使用其方法,微信已经抛弃。在学的时候根本无法进行调通,虽然这只是一个简单的服务器配置。但是给了很大的动力、

批改状态:未批改

老师批语:
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

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

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