버전 2.0이 새로 출시되었습니다. 현재 지원되는 로그인 플랫폼은 다음과 같습니다.
composer require anerg2046/sns_auth
클래스 라이브러리에서 사용되는 네임스페이스는
\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 微信多域名代理文件
인터페이스 파일에는 모든 타사 기본 클래스가 관련 타사 로그인 작업 및 데이터 획득을 위해 구현해야 하는 4가지 메서드가 정의되어 있습니다. 메소드 이름은 다음과 같습니다.
/**
* 得到跳转地址
*/
public function getRedirectUrl ();
/**
* 获取当前授权用户的openid标识
*/
public function openid ();
/**
* 获取格式化后的用户信息
*/
public function userinfo ();
/**
* 获取原始接口返回的用户信息
*/
public function userinfoRaw ();
WeChat에는 프록시가 요청한 주소를 얻기 위한 추가 방법이 있습니다.
/**
* 获取中转代理地址
*/
public function getProxyURL ();
ThinkPHP5를 예로 들어보겠습니다.
<?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 );
}
}
버전 2.0에서는 더 이상 시스템을 통해 상태를 자동으로 설정하지 않습니다. 필요한 경우 상태를 구성에 직접 배치할 수도 있습니다.
Line과 Facebook은 강제로 상태를 통과하게 됩니다. 설정하지 않으면 임의의 값이 전달됩니다.
상태를 확인하고 싶다면 사용자 정보 획득 시 ->mustCheckState()
메소드를 추가하면 됩니다.
$ snsInfo = OAuth:: $ name ( $ this -> config )-> mustCheckState ()-> userinfo ();
모든 플랫폼이 전달 상태를 지원하는 것은 아닙니다. 공식 문서를 직접 읽어보세요.
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는 이제 unionid
얻을 수 있습니다. 자세한 내용은 http://wiki.connect.qq.com/unionid%E4%BB%8B%E7%BB%8D를 참조하세요. 구성 매개변수 $config['withUnionid'] = true
만 해당됩니다. 필수, 기본값은 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에는 $config['field']
특별한 구성이 있습니다. 기본값은 'id,name,gender,picture.width(400)'
입니다. 공식 문서를 참조하여 원하는 사용자 정보를 선택할 수 있습니다. 필요합니다.
'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은 고유한 Unionid 필드를 반환합니다.
사용 중 문제가 있을 경우 이슈를 접수해 주시면 시간 내에 확인하도록 하겠습니다.