lcobucci/social

此包已被放弃,不再维护。未建议替代包。

PHP 5.3+ 的社交身份验证库

1.0.0-alpha3 2013-09-03 12:53 UTC

This package is not auto-updated.

Last update: 2022-02-01 12:26:55 UTC


README

PHP 5.3+ 的社交身份验证库

该项目不再维护

安装

应使用 composer 安装 Social。

基本用法

  1. 创建 AuthClient 并注册提供者
// client_config.php
require_once __DIR__ . '/vendor/autoload.php'; //requires the autoloader

use Lcobucci\Social\Providers\Github;
use Lcobucci\Social\AuthClient;
use Lcobucci\Social\Providers\Facebook;

$client = new AuthClient();

$client->appendProvider(
    'github', // The provider identifier (anything you may want) 
    Github::create(
        'blablabla', // The client ID
        'blablabla', // The client secret
        'http://blablabla.com/oauth/github/callback.php'// The callback URI  (if you want...)
        ['user:email'] // The default scopes (if you want...)
    )
);

return $client
  1. 重定向到提供者
// init.php
$client = require __DIR__ . '/client_config.php'; // Get your configuration
$uri = $client->getAuthorizationUri(
    'github', // The provider identifier you want to use (that you configured before)
    [], // Additional scopes (if you want...)
    uniqid() // State to be validated (if you want...)
);

header('Location: ' . $uri);
  1. 获取已认证用户信息
// callback.php
use Symfony\Component\HttpFoundation\Request;

$client = require __DIR__ . '/client_config.php'; // Get your configuration
$request = Request::createFromGlobals();

$user = $client->getAuthenticatedUser('github', $request->query);

var_dump($user->getToken()); // Get the access token that should be used on API requests
var_dump($user->getId()); // Get the user ID
var_dump($user->getUsername()); // Get the user login
var_dump($user->getName()); // Get the user name
var_dump($user->getEmail()); // Get the user email
var_dump($user->getAvatar()); // Get the user avatar