Git Product home page Git Product logo

socialite-easyswoole's Introduction

Socialite

Build Status Latest Stable Version Latest Unstable Version Build Status Scrutinizer Code Quality Code Coverage Total Downloads License

基于 overtrue/socialite改造的,适用于easyswoole的第三方登录组件,现已支持wechat,qq,weibo,github,facebook

依赖

PHP >= 7.0
swoole >=4.4.0

安装

$ composer require "xbing2002/socialite" "1.0"

使用说明

authorize.php:

<?php

use Overtrue\Socialite\SocialiteManager;

$config = [
    'wechat' => [
        'client_id'     => 'your-app-id',
        'client_secret' => 'your-app-secret',
        'redirect'      => 'http://localhost/socialite/callback.php',
    ],
];

$socialite = new SocialiteManager($config);

$socialite->driver('wechat')->redirect();

callback.php:

<?php

use Overtrue\Socialite\SocialiteManager;

$config = [
    'wechat' => [
        'client_id' => 'your-app-id',
        'client_secret' => 'your-app-secret',
        'redirect' => 'http://localhost/socialite/callback.php',
    ],
];

$socialite = new SocialiteManager($config);

$user = $socialite->driver('wechat')->user();

$user->getId();        // openid
$user->getNickname();  // "昵称"
$user->getName();      // "昵称"
$user->getAvatar();     // 头像
$user->getProviderName(); // WeChat
...

配置项

现在支持:

facebook, github, weibo, qq, wechat.

每一个登录平台的配置都是一样的,只需要配置: client_id, client_secret, redirect.

例子:

...
  'weibo' => [
    'client_id'     => 'your-app-id',
    'client_secret' => 'your-app-secret',
    'redirect'      => 'http://localhost/socialite/callback.php',
  ],
...

Scope

有些登录平台可以在跳转之前设置Scope:

$response = $socialite->driver('github')
                ->scopes(['scope1', 'scope2'])->redirect();

WeChat scopes:

  • snsapi_base, snsapi_userinfo - 用于公众号登录.
  • snsapi_login - 用户web登录.

跳转链接

当然你也可以动态设置跳转链接:

$socialite->redirect($url);
// or
$socialite->withRedirectUrl($url)->redirect();
// or
$socialite->setRedirectUrl($url)->redirect();

自定义参数

如果存在一些自定义参数,请用with方法

$response = $socialite->driver('google')
                    ->with(['hd' => 'example.com'])->redirect();

User interface

Standard user api:

$user = $socialite->driver('weibo')->user();
{
  "id": 1472352,
  "nickname": "overtrue",
  "name": "安正超",
  "email": "[email protected]",
  "avatar": "https://avatars.githubusercontent.com/u/1472352?v=3",
  "original": {
    "login": "overtrue",
    "id": 1472352,
    "avatar_url": "https://avatars.githubusercontent.com/u/1472352?v=3",
    "gravatar_id": "",
    "url": "https://api.github.com/users/overtrue",
    "html_url": "https://github.com/overtrue",
    ...
  },
  "token": {
    "access_token": "5b1dc56d64fffbd052359f032716cc4e0a1cb9a0",
    "token_type": "bearer",
    "scope": "user:email"
  }
}

你可以通过数组方式获取用户属性:

$user['id'];        // 1472352
$user['nickname'];  // "overtrue"
$user['name'];      // "安正超"
$user['email'];     // "[email protected]"
...

或者通过对象方式获取:

$user->getId();
$user->getNickname();
$user->getName();
$user->getEmail();
$user->getAvatar();
$user->getOriginal();
$user->getToken();// or $user->getAccessToken()
$user->getProviderName(); // GitHub/Google/Facebook...

你也可以直接获取各登录平台的原始返回数据

$user->getOriginal()

获取access token 对象

$user->getToken() $user->getAccessToken() $user['token']

可以通过access token 获取用户信息

$accessToken = new AccessToken(['access_token' => $accessToken]);
$user = $socialite->user($accessToken);

License

MIT

socialite-easyswoole's People

Contributors

ac1982 avatar andares avatar aufree avatar bangbangda avatar dependabot[bot] avatar eddiclin avatar flutterbest avatar hedeqiang avatar hotrush avatar khsing avatar littlesqx avatar mingyoung avatar overtrue avatar phoenixgao avatar summerblue avatar thisliu avatar xbing2002 avatar xiaohome avatar xjchengo avatar xofers avatar yuqi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.