Versi 2.0 baru saja dirilis. Platform login yang saat ini didukung meliputi:
composer require anerg2046/sns_auth
Namespace yang digunakan oleh perpustakaan kelas adalah
\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 微信多域名代理文件
Dalam file antarmuka, 4 metode ditentukan, yang harus diterapkan oleh setiap kelas dasar pihak ketiga untuk operasi login pihak ketiga terkait dan akuisisi data. Nama metodenya adalah sebagai berikut:
/**
* 得到跳转地址
*/
public function getRedirectUrl ();
/**
* 获取当前授权用户的openid标识
*/
public function openid ();
/**
* 获取格式化后的用户信息
*/
public function userinfo ();
/**
* 获取原始接口返回的用户信息
*/
public function userinfoRaw ();
WeChat memiliki metode tambahan untuk mendapatkan alamat yang diminta oleh proxy
/**
* 获取中转代理地址
*/
public function getProxyURL ();
Ambil ThinkPHP5 sebagai contoh
<?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 );
}
}
Versi 2.0 tidak lagi secara otomatis mengatur status melalui sistem. Jika perlu, harap tangani sendiri verifikasinya.
Line dan Facebook memerlukan status untuk diteruskan. Jika Anda tidak menyetelnya, nilai acak akan diteruskan.
Jika Anda ingin memverifikasi status, tambahkan metode ->mustCheckState()
saat memperoleh informasi pengguna.
$ snsInfo = OAuth:: $ name ( $ this -> config )-> mustCheckState ()-> userinfo ();
Perhatikan bahwa tidak semua platform mendukung passing state, harap baca sendiri dokumentasi resminya.
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 sekarang dapat memperoleh unionid
. Untuk detailnya, lihat: http://wiki.connect.qq.com/unionid%E4%BB%8B%E7%BB%8D Hanya parameter konfigurasi $config['withUnionid'] = true
yang diperlukan, defaultnya adalah Tidak akan meminta 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 memiliki konfigurasi khusus $config['field']
. Defaultnya adalah 'id,name,gender,picture.width(400)'
kebutuhan.
'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 akan mengembalikan bidang unionid unik
Jika Anda mengalami masalah saat penggunaan, silakan kirimkan masalahnya dan saya akan memeriksanya tepat waktu.