北京时时彩怎么玩法 www.t1xi6.cn 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 //thinkwww.t1xi6.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( //www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <[email protected]>
// +----------------------------------------------------------------------

// 应用公共文件
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白名单,添加配置文件