aiddroid/yii2-social

PHP 社交网络 OAuth 库

dev-master 2017-01-16 08:54 UTC

This package is auto-updated.

Last update: 2024-09-13 15:35:40 UTC


README

PHP 库,帮助您通过 OAuth 将网站与社交网络链接。

安装

1. 使用 Composer 安装

composer require 'aiddroid/yii2-social:dev-master'

使用方法

支持社交网络:facebook google github weibo wechat qq

1. 常规配置

<?php

//client config
$config = [
    'weibo' => [
        'client_id' => '384556****',
        'client_secret' => '507a34706a4cfffb87f7f06*****',
        'redirect_url' => 'https://.dev/callback.php',
        'addition' => [
            'scopes' => ['email']
        ]
    ],
    'qq' => [
        'client_id' => '10115****',
        'client_secret' => '2ee893df483e0a5a521d768683******',
        'redirect_url' => 'https://.dev/callback.php',
    ],
    'wechat' => [
        'client_id' => 'wx8b160f63e30*****',
        'client_secret' => 'f88eede680e8e6e7a7ea4b8******',
        'redirect_url' => 'https://.dev/callback.php',
    ],
    'github' => [
        'client_id' => '41473a449fb5d6******',
        'client_secret' => '5fea02f02998356b039b75f6d861f*******',
        'redirect_url' => 'https://.dev/callback.php'
    ],
    'facebook' => [
        'client_id' => '99544295*****',
        'client_secret' => '5ccff1d484c5ae417f7d1aa*******',
        'redirect_url' => 'https://.dev/callback.php'
    ],
    'google' => [
        'client_id' => '8983768*****-qf*******.apps.googleusercontent.com',
        'client_secret' => 'M5XhtghGBZC_Vwv*******',
        'redirect_url' => 'https://.dev/callback.php'
    ]
];

2. auth.php (应将用户重定向到第三方认证页面)

<?php
//github demo
$social = new Social($config);
$provider = $social->driver('github');
return $provider->redirect('https:///callback.php');

3. callback.php (如果认证成功,用户将被重定向到这个页面)

<?php
//github demo
$social = new Social($config);
$provider = $social->driver('github');
if(isset($_GET['code'])){
    $user = $provider->getUser();
}
{
    "id": 3241146,
    "nickname": "aiddroid",
    "avatar": "https://avatars.githubusercontent.com/u/3241146?v=3",
    "attributes": {
        "login": "aiddroid",
        "id": 3241146,
        "avatar_url": "https://avatars.githubusercontent.com/u/3241146?v=3",
        "gravatar_id": "",
        "url": "https://api.github.com/users/aiddroid",
        ......
        "created_at": "2013-01-11T04:08:15Z",
        "updated_at": "2016-12-13T07:46:50Z"
    }
}

4. 扩展您自己的社交网络提供者

  • 1. 从 Aiddroid\Social\Providers\AbstractProvider 扩展提供者

  • 2. 使用方法

    <?php
    
    $social = new Social($config);
    $social->setDriverProvider('dummysocial', Aiddroid\Social\Providers\DummysocialProvider::class);
    $provider = $social->driver('dummysocial');
    
    $provider->redirect('https://.dev/callback.php')

##如何贡献?您可以通过 GitHub pulls 贡献。任何帮助都受欢迎 (^_^)

##有任何疑问?请发送邮件至 aiddroid@gmail.com

##了解更多