微信公众平台之网页授权

此功能必须是服务号或者测试号,订阅号没有此权限。测试号可以在【开发】=》【开发者工具】=》【公众平台测试账号】申请,本文以测试号为例:

微信公众平台之网页授权

一:公众平台设置回调域名和测试账号

微信公众平台之网页授权

注:设置成你网站域名,不要加协议,举例:www.aaa.com,本地域名也可以

微信公众平台之网页授权

 

二:将下面Oauth.php放到此域名下(更改成你自己的appID和appsecret):

<?php  
class Oauth
{
    private $appID = 'xxxxxxx';
    private $appsecret = 'xxxxxxxxxxxxxxxxxxxxxxxx';

    private $code;
    private $token;
    private $openid;

    //日志记录
    private function _log($log_content){
        $log_filename = './wx';
        !is_dir($log_filename) && mkdir($log_filename, 0755, true);

        file_put_contents($log_filename.'/login.log', '['.date("Y-m-d H:i:s").']' .PHP_EOL . $log_content . PHP_EOL."------------------------ --------------------------".PHP_EOL, FILE_APPEND);
    }

    //获取token
    public function getToken() {
        $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$this->appID.'&secret='.$this->appsecret.'&code='.$this->code.'&grant_type=authorization_code';
        $token = $this->send($url);
        $this->_log('获取access_token:' . $this->ajax($token));
        if(isset($token['access_token'])) {
            $this->token = $token['access_token'];
            $this->openid = $token['openid'];
            return ['code'=>0, 'msg'=>'获取access_token成功'];
        }else{
            return ['code'=>1, 'msg'=>'获取access_token失败'];
        }
    }

    //获取用户信息
    function getUser() {
        $this->code = $_GET['code'];
        $token = $this->getToken();
        if($token['code']){
            header('location:/Oauth.php?method=power');
        }

        $url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$this->token.'&openid='.$this->openid.'&lang=zh_CN';
        $user = $this->send($url);
        $this->_log('获取用户信息:' . $this->ajax($user));
        if(!isset($user['openid'])) {
            $this->_log('获取用户信息失败!');
            echo "获取信息失败,请重新进入";die;
        }
        //入库存session[省略]
        return $this->ajax($user);
    }

    //网页授权
    function power() {
        //回调地址
        //协议
        $http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';
        $redirect_uri = urlencode($http_type.$_SERVER['SERVER_NAME'].'/Oauth.php?method=getUser');
        //请求授权
        header('location:https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$this->appID.'&redirect_uri='.$redirect_uri.'&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect');
    }

    /**
    * @param $url
    * @param null $_input
    * @param string $data_type
    * @return mixed
    * $_input= ["post"=>[],"refer"=>"",cookiefile='']
    */
    function send($url, $input=null, $data_type='json') {
        $ch = curl_init();
        $useragent = isset($input['useragent']) ? $input['useragent'] : 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.2) Gecko/20100101 Firefox/10.0.2';
        curl_setopt( $ch, CURLOPT_URL, $url );
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
        curl_setopt( $ch, CURLOPT_AUTOREFERER, true );
        curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
        curl_setopt( $ch, CURLOPT_POST, isset($input['post']) );
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //不验证证书
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //不验证证书
        if( isset($input['post']) ) curl_setopt( $ch, CURLOPT_POSTFIELDS, $input['post'] );
        if( isset($input['refer']) ) curl_setopt( $ch, CURLOPT_REFERER, $input['refer'] );
        curl_setopt( $ch, CURLOPT_USERAGENT, $useragent );
        curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, ( isset($input['timeout']) ? $input['timeout'] : 5 ) );
        $result = curl_exec( $ch );
        curl_close( $ch );
        if ($data_type == 'json') {
            $result = json_decode($result,true);
        }
        return $result;
    }

    function ajax($data)
    {
        return json_encode($data, JSON_UNESCAPED_UNICODE);
    }

}

$oauth = new Oauth();
$method = isset($_GET['method'])?$_GET['method']:'power';
echo $oauth->$method();

 

三:打开微信开发者工具,访问【域名/Oauth.php】

微信公众平台之网页授权

上一篇:jquery获取不到append出来的新元素的解决办法


下一篇:知晓微信域名被封的主要原因