Version 2.0 is newly released. Currently supported login platforms include:
composer require anerg2046/sns_auth
The namespace used by the class library is
\anerg\OAuth2
.
├── README.md 说明文件
├── composer.json composer文件
├── src 代码源文件目录
│ ├── Connector
│ │ ├── Gateway.php 必须继承的抽象类
│ │ └── GatewayInterface.php 必须实现的接口
│ ├── Gateways
│ │ ├── Alipay.php
│ │ ├── Facebook.php
│ │ ├── Google.php
│ │ ├── Line.php
│ │ ├── Qq.php
│ │ ├── Twitter.php
│ │ ├── Weibo.php
│ │ └── Weixin.php
│ ├── Helper
│ │ └── Str.php 字符串辅助类
│ └── OAuth.php 抽象实例类
└── wx_proxy.php 微信多域名代理文件
In the interface file, 4 methods are defined, which every third-party base class must implement for related third-party login operations and data acquisition. The method names are as follows:
/**
* 得到跳转地址
*/
public function getRedirectUrl ();
/**
* 获取当前授权用户的openid标识
*/
public function openid ();
/**
* 获取格式化后的用户信息
*/
public function userinfo ();
/**
* 获取原始接口返回的用户信息
*/
public function userinfoRaw ();
WeChat has an additional method for obtaining the address requested by the proxy
/**
* 获取中转代理地址
*/
public function getProxyURL ();
Take ThinkPHP5 as an example
<?php
namespace app mobile controller ;
use anerg OAuth2 OAuth ;
use think facade Config ;
class Sns
{
private $ config ;
/**
* 第三方登录,执行跳转操作
*
* @param string $name 第三方渠道名称,目前可用的为:weixin,qq,weibo,alipay,facebook,twitter,line,google
*/
public function login ( $ name )
{
//获取配置
$ this -> config = Config:: get ( ' sns. ' . $ name );
//设置回跳地址
$ this -> config [ ' callback ' ] = $ this -> makeCallback ( $ name );
//可以设置代理服务器,一般用于调试国外平台
$ this -> config [ ' proxy ' ] = ' http://127.0.0.1:1080 ' ;
/**
* 对于微博,如果登录界面要适用于手机,则需要设定->setDisplay('mobile')
*
* 对于微信,如果是公众号登录,则需要设定->setDisplay('mobile'),否则是WEB网站扫码登录
*
* 其他登录渠道的这个设置没有任何影响,为了统一,可以都写上
*/
return redirect (OAuth:: $ name ( $ this -> config )-> setDisplay ( ' mobile ' )-> getRedirectUrl ());
/**
* 如果需要微信代理登录,则需要:
*
* 1.将wx_proxy.php放置在微信公众号设定的回调域名某个地址,如 http://www.abc.com/proxy/wx_proxy.php
* 2.config中加入配置参数proxy_url,地址为 http://www.abc.com/proxy/wx_proxy.php
*
* 然后获取跳转地址方法是getProxyURL,如下所示
*/
$ this -> config [ ' proxy_url ' ] = ' http://www.abc.com/proxy/wx_proxy.php ' ;
return redirect (OAuth:: $ name ( $ this -> config )-> setDisplay ( ' mobile ' )-> getProxyURL ());
}
public function callback ( $ name )
{
//获取配置
$ this -> config = Config:: get ( ' sns. ' . $ name );
//设置回跳地址
$ this -> config [ ' callback ' ] = $ this -> makeCallback ( $ name );
//获取格式化后的第三方用户信息
$ snsInfo = OAuth:: $ name ( $ this -> config )-> userinfo ();
//获取第三方返回的原始用户信息
$ snsInfoRaw = OAuth:: $ name ( $ this -> config )-> userinfoRaw ();
//获取第三方openid
$ openid = OAuth:: $ name ( $ this -> config )-> openid ();
}
/**
* 生成回跳地址
*
* @return string
*/
private function makeCallback ( $ name )
{
//注意需要生成完整的带http的地址
return url ( ' /sns/callback/ ' . $ name , '' , ' html ' , true );
}
}
Version 2.0 no longer automatically sets the state through the system. If necessary, please handle the verification yourself. The state can also be placed in the config.
Line and Facebook require state to be passed. If you do not set it, a random value will be passed.
If you want to verify the state, add the ->mustCheckState()
method when obtaining user information.
$ snsInfo = OAuth:: $ name ( $ this -> config )-> mustCheckState ()-> userinfo ();
Note that not all platforms support passing state, please read the official documentation yourself.
public function sns ()
{
$ platform = $ this -> request -> param ( ' sns_platform ' );
//获取本站的第三方登录配置
$ config = Config:: get ( $ platform . ' . ' . Config:: get ( $ platform ));
// $config['proxy'] = 'http://127.0.0.1:1080';
//QQ,Facebook,Line,要求客户端传递access_token即可
$ config [ ' access_token ' ] = $ this -> request -> param ( ' access_token ' , '' );
//Twitter需要传递下面四个参数
$ config [ ' oauth_token ' ] = $ this -> request -> param ( ' oauth_token ' , '' );
$ config [ ' oauth_token_secret ' ] = $ this -> request -> param ( ' oauth_token_secret ' , '' );
$ config [ ' user_id ' ] = $ this -> request -> param ( ' user_id ' , '' );
$ config [ ' screen_name ' ] = $ this -> request -> param ( ' screen_name ' , '' );
//其他和web登录一样,要求客户端传递code过来即可,可以是post也可以是get方式
$ snsInfo = OAuth:: $ platform ( $ config )-> userinfo ();
print_r ( $ snsInfo );
}
'app_id' => 'wxbc4113c******',
'app_secret' => '4749970d284217d0a**********',
'scope' => 'snsapi_userinfo',//如果需要静默授权,这里改成snsapi_base,扫码登录系统会自动改为snsapi_login
'app_id' => '1013****',
'app_secret' => '67c52bc284b32e7**********',
'scope' => 'get_user_info',
QQ can now obtain unionid
. For details, see: http://wiki.connect.qq.com/unionid%E4%BB%8B%E7%BB%8D Only the configuration parameter $config['withUnionid'] = true
is required, the default is Will not request Unionid
'app_id' => '78734****',
'app_secret' => 'd8a00617469018d61c**********',
'scope' => 'all',
'app_id' => '2016052*******',
'scope' => 'auth_user',
'pem_private' => Env::get('ROOT_PATH') . 'pem/private.pem', // 你的私钥
'pem_public' => Env::get('ROOT_PATH') . 'pem/public.pem', // 支付宝公钥
'app_id' => '2774925********',
'app_secret' => '99bfc8ad35544d7***********',
'scope' => 'public_profile,user_gender',//user_gender需要审核,所以不一定能获取到
Facebook has a special configuration $config['field']
. The default is 'id,name,gender,picture.width(400)'
. You can refer to the official documentation to select the user information you want to obtain according to your needs.
'app_id' => '3nHCxZgcK1WpYV**********',
'app_secret' => '2byVAPayMrG8LISjopwIMcJGy***************',
'app_id' => '159******',
'app_secret' => '1f19c98a61d148f2************',
'scope' => 'profile',
'app_id' => '7682717*******************.apps.googleusercontent.com',
'app_secret' => 'w0Kq-aYA***************',
'scope' => 'https://www.googleapis.com/auth/userinfo.profile',
Array
(
[openid] => 1047776979*******
[channel] => google
[nick] => Coeus Rowe
[gender] => m //twitter和line不会返回性别,所以这里是n,Facebook根据你的权限,可能也不会返回,所以也可能是n
[avatar] => https://lh6.googleusercontent.com/-iLps1iAjL8Q/AAAAAAAAAAI/AAAAAAAAAAA/Bu5l0EIquF0/photo.jpg
)
WeChat will return the unique unionid field
If you have any problems during use, please submit an issue and I will check it in time.