PHP code example of barbery / social-login

1. Go to this page and download the library: Download barbery/social-login 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/ */

    

barbery / social-login example snippets




// 声明包引用
use Barbery\SocialLogin\SocialLogin;

class User extend Controllers
{
    # 其他Action....

    /**
     * @name 第三方登陆统一网关
     * @method GET
     * @param string redirectUrl Y 第三方登陆完毕跳回地址
     * @return mixed
     */
    public function socialLoginAction()
    {
        // 配置可以移到框架的配置文件里面
        $config = [
            'proxy' => [
                'dns' => '127.0.0.1:1080',
                'type' => CURLPROXY_SOCKS5
            ],
            'weibo' => [
                'key' => '111111111',
                'secret' => 'xxxxxxxxx',
                'redirectUrl' => '/user/socialLogin?type=weibo',
                'scope' => 'email'
            ],
            'qq' => [
                'key' => '111111111',
                'secret' => 'xxxxxxxxxxxx',
                'redirectUrl' => '/user/socialLogin?type=qq',
            ],
            'google' => [
                'key' => 'xxxxxxxxxxxxxx',
                'secret' => 'xxxxxxxxxx',
                'redirectUrl' => '/user/socialLogin?type=google',
                'scope' => 'https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile',
                'enableProxy' => true,
            ],
            'github' => [
                'key' => 'xxxxxxxxxxxx',
                'secret' => 'xxxxxxxxxxxxx',
                'redirectUrl' => '/user/socialLogin?type=github',
            ],
        ];


        try {
            $obj = new SocialLogin($config);
            $socialInfo = $obj->authAndGetUserInfo();
        } catch (Exception $e) {
            echo $e->getMessage(), '具体原因:' . print_r($obj->getLastError(), true);
            exit;
        }

        print_r($socialInfo);
            // Array
            // (
            //     [nickname] => 偶尔陶醉
            //     [face] => http://tp1.sinaimg.cn/1763035100/180/5684566873/1
            //     [gender] => 1
            //     [openid] => 11111111
            //     [access_token] => xxxxxxxxxxxxxx
            //     [timeout] => 1600413552
            //     [refresh_token] => 
            //     [from] => weibo
            // )
        
        if ( ! User::isExist($socialInfo['openid'], $socialInfo['from'])) {
            # 如果用户不存在, 引导用户完成后续注册
        } else {
            # 用户存在,则完成登陆
            redirect($_GET['redirectUrl']);
        }
    }


    # 其他Action....
}


[
    // 支持代理设置,国内的GFW,登陆google时你懂的
    'proxy' => [
        'dns' => '127.0.0.1:1080',
        //代理类型,具体请看 curl CURLOPT_PROXYTYPE 选项说明:http://php.net/manual/zh/function.curl-setopt.php 
        'type' => CURLPROXY_SOCKS5
    ],
    // 第三方登陆应用设置
    // 服务商
    'weibo' => [
        //应用信息
        'key' => '11111111111',
        'secret' => 'xxxxxxxxxxxxxxx',
        // 授权返回地址,注意:这里需要跳回到统一的第三方处理action, 其中type参数要和服务商对应不可缺失
        'redirectUrl' => '/user/socialLogin?type=weibo',
        // 授权内容
        'scope' => 'email'
    ],
    'qq' => [
        'key' => '11111111111',
        'secret' => 'xxxxxxxxxxxxxxx',
        'redirectUrl' => 'http://cvmeta.com/user/socialLogin?type=qq',
    ],
    'google' => [
        'key' => 'xxxxxxxxxxxxxxx',
        'secret' => 'xxxxxxxxxxxxxxx',
        'redirectUrl' => '/user/socialLogin?type=google',
        'scope' => 'https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile',
        // 是否启用代理
        'enableProxy' => true,
    ],
    'github' => [
        'key' => 'xxxxxxxxxxxxxxx',
        'secret' => 'xxxxxxxxxxxxxxx',
        'redirectUrl' => '/user/socialLogin?type=github',
    ],
];


$userInfo = array(
    'nickname'      => '第三方昵称',
    'face'          => '第三方头像url,优先拿大图',
    'gender'        => '性别',//1为男,2为女,0为未设置或未知
    'openid'        => '第三方唯一身份标识id',
    'access_token'  => '授权access_token',
    'timeout'       => 'access_token过期时间戳',
    'refresh_token' => 'refresh_token','如果有则返回,无则为空字符串',
    'from'          => '第三方来源标识'//google,github,weibo,qq...etc.
);

array(
    'code' => '第三方登陆服务商返回错误码',//如果发生错误且服务商没有返回错误码则为-1,没有错为0
    'msg' => '第三方登陆服务商返回的错误msg'
);