博主信息
博文 9
粉丝 0
评论 0
访问量 6488
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
5月31微信服务器对接生成数字签名程序
张凯的博客
原创
847人浏览过
controller 控制器文件 Weixin.php

<?php

/**
 * @Author: owlcity
 * @Date:   2018-06-01 14:43:19
 * @Last Modified by:   owlcity
 * @Last Modified time: 2018-06-03 22:16:48
 */
namespace app\index\controller;
use think\Controller;
use think\facade\Cache;

class Weixin extends Controller{
   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'));
    }
    // 获取access_token
    public function get_access_token($iscache=true){
        // 查看缓存access_token
        $cache_key = 'access_token';
        if(!iscache){
            Cache::rm($cache_key);
        }
        $access_token = Cache::get($cache_key);

        if($access_token && $iscache){
            return $access_token;
        }

        $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);
        // json_decode 第二个参数默认false,为 TRUE 时,将返回数组,FALSE 时返回对象
        $res = json_decode($res,true);
        if(!isset($res['access_token'])){
            return false;
        }
        // dump($res);
        // 将access_token进行缓存
        // Cache::set(key,value,time)time:为过期时间默认永久
        Cache::set($cache_key,$res['access_token'],$res['expires_in']=600);
        dump($res);
    }
}

Model 文件

<?php

/**
 * @Author: owlcity
 * @Date:   2018-06-01 18:14:51
 * @Last Modified by:   owlcity
 * @Last Modified time: 2018-06-03 22:05:12
 */
namespace app\index\model;
use think\Model;

class Weixin extends Model{
    // 校验签名
    public function valid(){
        $signature = input('get.signature');
        $timestamp = input('get.timestamp');
        $nonce = input('get.nonce');
        $token = config('app.weixintoken');
        $tmpArr = array($timestamp,$nonce,$token);
        sort($tmpArr, SORT_STRING);
        $str = implode($tmpArr);
        // dump($tmpArr);
        if(sha1($str) != $signature){
            return false;
        }
        return true; 


    }
}

公共函数库 common.php

<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
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);
    //启用时会发送一个常规的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);//需要要传送的内容
    }
    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');//需要要传送的内容
    curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
    $return_str = curl_exec($curl);
    curl_close($curl);
    return $return_str;
}

注意事项: 订阅号ip白名单,添加配置文件


批改状态:未批改

老师批语:
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系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+教程免费学