微信登錄通常是通過OAuth2.0來授權(quán)的她按。
- 客戶端向微信服務(wù)端發(fā)起請求獲取code歉摧;
- 獲取到code后向服務(wù)端發(fā)起請求登錄撵摆,
- 服務(wù)端通過code向微信服務(wù)器換取網(wǎng)頁授權(quán)access_token和用戶的openID蜕衡,
- 再通過access_token獲取用戶信息李滴,保存到服務(wù)端突硝。
laravel中微信登錄有集成的擴展测摔,
socialiteproviders
- 安裝擴展
composer require socialiteproviders/weixin
- 配置
app/Providers/EventServiceProvider.php
protected $listen = [
\SocialiteProviders\Manager\SocialiteWasCalled::class => [
// add your listeners (aka providers) here
'SocialiteProviders\Weixin\WeixinExtendSocialite@handle'
],
];
config/services.php
'weixin' => [
'client_id' => env('WEIXIN_KEY'),
'client_secret' => env('WEIXIN_SECRET'),
'redirect' => env('WEIXIN_REDIRECT_URI')
],
.env
# 微信
WEIXIN_KEY=wxbde46abaa8d3xx
WEIXIN_SECRET=ae0059ed19c9367xxxx
WEIXIN_REDIRECT_URI=http://larxxx
- 示例
# 測試路由
Route::get('test', function () {
$code = '081euRI705A3iF1MmoF70TNQI70euRIt';
$driver = Socialite::driver('weixin');
$response = $driver->getAccessTokenResponse($code);
$driver->setOpenId($response['openid']);
$oauthUser = $driver->userFromToken($response['access_token']);
dd($oauthUser);
});