PHP code example of jonexyz / qqconnect

1. Go to this page and download the library: Download jonexyz/qqconnect library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jonexyz / qqconnect example snippets

$xslt
use Jonexyz\QQConnect\QC;

// 回调操作
    public function qqCallback(Request $request)
    {
        $qc = new QC($appid,$appkey,$callback,$scope);

        if(!$request->get('code')){
            if(cache('QC_userData')['openid']){
                $qc->setKeysArr($access_token, $openid);
                $info = $qc->get_user_info();
                $data = [
                    'name'=>$info['nickname'], //昵称
//                    ''=>$info['gender_type'], // 性别 1男
//                    ''=>$info['province'], //省份
//                    ''=>$info['city'], //城市
//                    ''=>$info['figureurl_2'], //头像
//                    ''=>$info['year'], // 出生年分
                ];
                
                // TODO 信息获取成功,保存用户数据,实现用户登录

                // 进入登陆成功页
                return redirect(url('user'));
            }else{
                // 授权回调失败,显示404
                abort(404);
            }
        }else{
            $access_token = $qc->qq_callback();
        
            if($access_token){
                $openid = $qc->get_openid();

                $user = User::where('qq_openid',$openid)->first();

                if($user){
                    // TODO 信息获取成功,保存用户数据,实现用户登录
                    
                    // 缓存数据,access_token有效期30天
                    cache('QC_userData',['access_token'=>$access_token,'openid'=>$openid], 29*24*3600);
                    // 进入登陆成功页
                    return redirect(url('user'));
                }else{
                    // 重新访问回调地址
                    return redirect($callback);
                }

            }else{
                return abort(404);
            }
        }
    }