菜鸟微信填坑一

微信连接验签和微信网页授权

$http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';
define('WEB_PATH', $http_type.$_SERVER["HTTP_HOST"]);
<?php
namespace app\wx\controller;


use think\Controller;

use think\Db;
use think\facade\Cache;

class Api extends Controller
{
    public $token = "zjy921210";
    public $website = WEB_PATH;
    public function open(){
        //微信连接
        $this->valid();

    }
//https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx75a6aa0068171c6c&redirect_uri=https%3a%2f%2fweixin.shinycg.com%2fsites%2fhd_hyd_sign%2fpublic%2findex.php%2fwx%2fapi%2fauth&response_type=code&scope=snsapi_base&state=200#wechat_redirect
    public function url()
    {
        $url =urlencode("https://weixin.shinycg.com/sites/hd_hyd_sign/public/index.php/wx/api/auth");
        header("Location:https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx75a6aa0068171c6c&redirect_uri={$url}&response_type=code&scope=snsapi_base&state=200#wechat_redirect");
    }
    //获取授权用户信息
    public function auth()
    {

        $code = input('get.code');
        //比较code防止失效
        if(!Cache::get('code')){
            Cache::set('code',$code);
        }else{
            if(Cache::get('code')==$code){
                Cache::rm('code');
                header("Location:https://weixin.shinycg.com/sites/hd_hyd_sign/public/index.php/wx/api/url");
            }else{
                Cache::set('code',$code);
            }
        }
        if($code){
            $appid = "wx75a6aa0068171c6c";
            $secret = "91413fe5c38e7a8675ae93a1fb23879e";
            //换取access_token
            $oauth2Url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$appid}&secret={$secret}&code={$code}&grant_type=authorization_code";
            $oauth2 = $this->getJson($oauth2Url);
            $refresh_token = $oauth2["refresh_token"];
            $get_user_info_url="https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=".$appid."&grant_type=refresh_token&refresh_token=".$refresh_token;
            $userinfo = $this->getJson($get_user_info_url);
            $openid = $userinfo['openid'];
            header("Location:http://weixin.shinycg.com/sites/hd_hyd_sign/public/frontend/#/index?openid={$openid}");
        }else{
            header("Location:https://weixin.shinycg.com/sites/hd_hyd_sign/public/index.php/wx/api/url");
        }
    }
    //------------------------------------------------------------------------------------
    //微信接口连接
    public function valid(){
        $echoStr = input('get.echostr');
        //valid signature , option
        if($this->checkSignature()){
            echo $echoStr;
            return true;
        }
        return false;
    }
    private function checkSignature(){
        $signature = input('get.signature');
        $timestamp = input('get.timestamp');
        $nonce     = input('get.nonce');
        $token     = $this->token;
        $tmpArr    = array($token, $timestamp, $nonce);
        sort($tmpArr, SORT_STRING);
        $tmpStr    = implode( $tmpArr );
        $tmpStr    = sha1( $tmpStr );
        if( $tmpStr == $signature ){
            return true;
        }else{
            return false;
        }
    }

    function getJson($url){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($ch);
        curl_close($ch);
        return json_decode($output, true);
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值