php QQ登录实例(复制可用)

IT-Pony 2016-03-22 AM 507℃ 0条
    //qq登录
    $code = empty($_GET['code'])? '' : trim($_GET['code']);
    //判断是不是手机登录
    if(isMobile() == 1){
        $pc_state = 'oauth2.0';
        $url_state = '';
    }else{
        $pc_state = 'moc2';
        $url_state = 'z.';
    }
    //配置文件 官方提供的id
    $appid = '*********';
    //配置文件 官方提供的appkey
    $appkey = '*************************';
    //返回地址
    $callback = 'https://localhost';
    //请求地址
    $url = 'https://graph.'.$url_state.'qq.com/'.$pc_state.'/authorizeresponse_type=code&client_id='.$appid.'&redirect_uri='.$callback;
    //通过code码兑换Access_Token
    $token_url = 'https://graph.'.$url_state.'qq.com/'.$pc_state.'/token?grant_type=authorization_code&client_id='.$appid.'&client_secret='.$appkey.'&code='.$code.'&redirect_uri='.$callback;
    //获取用户唯一标识 openid
    $open_id_url = 'https://graph.'.$url_state.'qq.com/'.$pc_state.'/me?access_token=';
    //判断如果code码不为空的话,执行
    if(!empty($code)){
        //截取请求返回的Access_token
        $str_access = explode('&',https_get($token_url));
        $access_token = explode('=',$str_access[0]);
        //兑换openid (用户唯一标识)
        $qq_info = @https_get($open_id_url.$access_token[1]);
        $openid = json_decode(explode(')',explode('(',$qq_info)[1])[0],true);
        //将 $openid['openid'] 存入数据库中
        die($openid['openid']);
    }
    die(header('Location:'.$url));
标签: none

非特殊说明,本博所有文章均为博主原创。

评论啦~