PHP code example of shopwwi / webman-socialite

1. Go to this page and download the library: Download shopwwi/webman-socialite 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/ */

    

shopwwi / webman-socialite example snippets


   'driver' => [
      ...
      'qq' => [
          'provider' => \Shopwwi\WebmanSocialite\Providers\QqProvider::class,
          'client_id' => '',
          'client_secret' => '',
          'redirect' => 'http://your-callback-url',
      ],
      ...
   ]



namespace app\controller\auth;

use support\Request;
use Shopwwi\WebmanSocialite\Facade\Socialite;

class QqController
{
    /**
     * 将用户重定向到 QQ 的授权页面
     *
     * @return 
     */
    public function redirect(Request $request)
    {
        $redirect = Socialite::driver('qq')->redirect();
        return redirect($redirect);
    }

    /**
     * 从 QQ 获取用户信息
     *
     */
    public function callback(Request $request)
    {
         $code = $request->input('code');
         
         $qqUser = Socialite::driver('qq')->userFromCode($code);
         
         //示例
         $user = User::where('qq_id', $qqUser->id)->first();
         if ($user) {
           $user->update([
               'qq_token' => $qqUser->access_token,
               'qq_refresh_token' => $qqUser->refresh_token,
           ]);
         } else {
           $user = User::create([
               'name' => $qqUser->name,
               'email' => $qqUser->email,
               'qq_id' => $qqUser->id,
               'qq_token' => $qqUser->access_token,
               'qq_refresh_token' => $qqUser->refresh_token,
           ]);
         }
          Auth::login($user);
          return redirect('/index');
    }
}

'driver' => [
    ..
    'line' => [
        'provider' => \app\provider\LineProvider::class, 
        'client_id' => 'your-app-id',
        'client_secret' => 'your-app-secret',
        'redirect' => 'http://your-callback-url',
    ],
    ..
];

$socialite = Socialite::driver('line')->redirect();
   

$config = [
    'line' =>[
        'provider' => 'line',
        'client_id' => 'your-app-id',
        'client_secret' => 'your-app-secret',
        'redirect' => 'http://your-callback-url',
    ]   
];
$socialite = Socialite::config(new \Shopwwi\WebmanSocialite\Config($config))->extend('line', function(array $config) {
    return new LineProvider($config);
})->driver('line')->redirect();

// 下面直接注入也是可以的哈
$config = [
    'line' =>[
        'provider' => \app\provider\LineProvider::class,
        'client_id' => 'your-app-id',
        'client_secret' => 'your-app-secret',
        'redirect' => 'http://your-callback-url',
    ]   
];
$socialite = Socialite::config(new \Shopwwi\WebmanSocialite\Config($config))->driver('line')->redirect();


    namespace app\provider;
    class LineProvider implements \Shopwwi\WebmanSocialite\Contracts\Provider
    {
        //...
    }


    namespace app\provider;
    use Shopwwi\WebmanSocialite\Providers\AbstractProvider;
    use Shopwwi\WebmanSocialite\Contracts;
    use Shopwwi\WebmanSocialite\AbstractUser;
    class LineProvider extends AbstractProvider
    {
        public const NAME = 'line';
    
        protected string $baseUrl = 'https://api.line.me/oauth2/';
    
        protected string $version = 'v2.1';
    
        protected array $scopes = ['profile'];
    
        protected function getAuthUrl(): string
        {
            $this->state = $this->state ?: \md5(\uniqid(Contracts\SHOPWWI_SOC_STATE, true));
    
            return $this->buildAuthUrlFromBase('https://access.line.me/oauth2/'.$this->version.'/authorize');
        }
    
        protected function getTokenUrl(): string
        {
            return $this->baseUrl.$this->version.'/token';
        }
    
        /**
         * @param string $code
         * @return array
         */
        protected function getTokenFields(string $code): array
        {
            return parent::getTokenFields($code) + [Contracts\SHOPWWI_SOC_GRANT_TYPE => Contracts\SHOPWWI_SOC_AUTHORIZATION_CODE];
        }
    
        /**
         * @param string $token
         * @return array
         * @throws \GuzzleHttp\Exception\GuzzleException
         */
        protected function getUserByToken(string $token): array
        {
            $response = $this->getHttpClient()->get(
                'https://api.line.me/v2/profile',
                [
                    'headers' => [
                        'Accept' => 'application/json',
                        'Authorization' => 'Bearer '.$token,
                    ],
                ]
            );
    
            return $this->fromJsonBody($response);
        }
    
        /**
         * @param array $user
         * @return Contracts\User
         */
        protected function mapUserToObject(array $user): Contracts\User
        {
            return new AbstractUser([
                Contracts\SHOPWWI_SOC_ID => $user['userId'] ?? null,
                Contracts\SHOPWWI_SOC_NAME => $user['displayName'] ?? null,
                Contracts\SHOPWWI_SOC_NICKNAME => $user['displayName'] ?? null,
                Contracts\SHOPWWI_SOC_AVATAR => $user['pictureUrl'] ?? null,
                Contracts\SHOPWWI_SOC_EMAIL => null,
            ]);
        }
    }

$code = \request()->input('code');
$user = Socialite::driver('alipay')->userFromCode($code);

// 详见文档后面 "User interface"
$user->getId();        
$user->getNickname();
$user->getUsername();
$user->getName();
...


$code = \request()->input('code');
$user = Socialite::driver('dingtalk')->userFromCode($code);

// 详见文档后面 "User interface"
$user->getId();       
$user->getNickname();  
$user->getUsername(); 
$user->getName(); 
...


$code = \request()->input('code');
$user = Socialite::driver('douyin')->userFromCode($code);
// 通过access_token获取用户信息时,需先设置openId
$openId = '4154d454..5561';
$token = '';
$user = Socialite::driver('douyin')->withOpenId($openId)->userFromToken($token);


$code = \request()->input('code');
$user = Socialite::driver('toutiao')->userFromCode($code);
// 通过access_token获取用户信息时,需先设置openId
$openId = '4154d454..5561';
$token = '';
$user = Socialite::driver('toutiao')->withOpenId($openId)->userFromToken($token);



$code = \request()->input('code');
$user = Socialite::driver('toutiao')->userFromCode($code);
//通过access_token获取用户信息时,需先设置openId
$openId = '4154d454..5561';
$token = '';
$user = Socialite::driver('toutiao')->withOpenId($openId)->userFromToken($token);


$authUrl = Socialite::driver('baidu')->withDisplay('mobile')->redirect();



$code = \request()->input('code');
$user = Socialite::driver('feishu')->withInternalAppMode()->userFromCode($code);

$appTicket = '';
$code = \request()->input('code');
$user = Socialite::driver('feishu')->withDefaultMode()->withAppTicket($appTicket)->userFromCode($code);



$code = \request()->input('code');
$user = Socialite::driver('lark')->withInternalAppMode()->userFromCode($code);

$appTicket = '';
$code = \request()->input('code');
$user = Socialite::driver('lark')->withDefaultMode()->withAppTicket($appTicket)->userFromCode($code);



$authUrl = Socialite::driver('taobao')->withView('wap')->redirect();

     'wechat' => [
         'provider' => \Shopwwi\WebmanSocialite\Providers\WeChatProvider::class,
         'client_id' => 'your-app-id',
         'client_secret' => 'your-app-secret',
         'redirect' => 'http://your-callback-url',
         // 开放平台 - 第三方平台所需
         'component' => [
             // or 'app_id', 'component_app_id' as key
             'id' => 'component-app-id',
             // or 'app_token', 'access_token', 'component_access_token' as key
             'token' => 'component-access-token',
         ]
     ],


$redirect = Socialite::driver('qq')->scopes(['scope1', 'scope2'])->redirect();


$url = 'your callback url.';

Socialite::driver('qq')->redirect($url);
// or
Socialite::driver('qq')->withRedirectUrl($url)->redirect();


session_start();
 
// Assign to state the hashing of the session ID
$state = hash('sha256', session_id());

$url = Socialite::driver('qq')->withState($state)->redirect();

return redirect($url); 


session_start();
 
$state = \request()->input('state');
$code = \request()->input('code');
 
// Check the state received with current session id
if ($state != hash('sha256', session_id())) {
    exit('State does not match!');
}
$user = Socialite::driver('qq')->userFromCode($code);

// authorized

$response = Socialite::driver('qq')->with(['hd' => 'example.com'])->redirect();

$user = Socialite::driver('qq')->userFromCode($code);

$user['id']; 
$user['nickname'];
$user['name']; 
$user['email'];
...


$user->getId();
$user->getNickname();
$user->getName();
$user->getEmail();
$user->getAvatar();
$user->getRaw();
$user->getAccessToken(); 
$user->getRefreshToken();
$user->getExpiresIn();
$user->getTokenResponse();


$accessToken = 'xxxxxxxxxxx';
$user = $socialite->userFromToken($accessToken);