aiddroid/social

PHP社交网络OAuth库

v0.3 2016-12-18 03:11 UTC

This package is auto-updated.

Last update: 2024-09-13 15:37:46 UTC


README

一个PHP库,帮助您通过OAuth将网站与社交网络连接起来。

安装

1. 通过Composer安装

composer require 'aiddroid/social:dev-master'

用法

支持的社交网络:Facebook Google GitHub 微博 微信 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的pull请求进行贡献。任何帮助都受欢迎(^_^)

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

##阅读更多