? ??? PHP ?? laravel ????? ???? ???? ?? ? ?????? ???? ?? ??? ?? WeChat ??? ??? ?????. ??? ???? ??:
1. php_weixin_provider ??
???? ????? ??? ????? ???? ??? thirdproviders/weixin? ?????. ????? ???? ????? ???? ?????? php_weixin_provider ????? ??? ? ? ????.
2. WeChat ??? ???? ??
??? ? ?? ????? ? 7????.
client_id: ?? ???? ??? ?????? appid? ??
client_secret: ?? ???? ??? ?????? appid? ??
????: WeChat ?? ?? ? ?? ??? ?????.
Proxy_url: WeChat?? ??? ??? ??? ??? ?????(?? ??? ????? ? ??? ?????).
??: PC??? WeChat ???? ?????? WeChat ???? ???? ???? PC???.
state_cookie_name: ?? ???? ??? ?? ????? ?????. ? ????? WeChat? ??? ? ??? ?????. ?? ?? ????? ??? ????? ???? ???? ??? ???? ??? ??? ? ????. ?? ??. CSRF ??? ?????. ? ???? ?? ?? ?? ?? ?? ????? ??? ????? ? ????? ?? ????? ???? ??? ??? ???? ? ?????. ???? wx_state_cookie???.
state_cookie_time: wx_state_cookie? ?? ??? ???? ???? 5????.
? 7? ?????? 2?? ?? ??? ????.
? ??? .env ?? ???? ??? ????? ???? ???? ????.
??: 1. ? ?? ??? ???? ???? WEIXIN_?? ?????. 2. ?? ? ?? ?? ??? ??? ??? ???? ??? ??? ???? ????. KEY? client_id? ????, SECRET? client_secret? ????, REDIRECT_URI? ????? ?????. ?? ??? ??? ??? ???? ??? ?????.
? ??? ??? ????? config/services.php ??? ???? ????:
??? ???? ????? ? ?? ??? ??? ??? ??? ?? ?????.
?? ??:
php_weixin_provider? laravel/socialite ???? ???? ??? client_id, client_secret, Redirect? ??? ???? ???. ??? ??? client_id? client_secret? ?? php_weixin_provider ????? ???? ??? ????? ??? ?????? ??? ?? ? ????. ??? ????? ?? ???? ???? ?? ????? ?? ??? ???? ?? ? ????. ???? WeChat ???? ???? ?? ??? ??? ?????. ?? ?? ??? ????? ????? ??? ??? ???? ?? ????. ?? ?? ?? ??? ?? ??; ??? ??? php_weixin_provider? ??? ? ??? ? ? ????? ?? ??? ?? ????.
Proxy_url? ???? ?? ?? ???? ???? ?? ????.
state_cookie_name? state_cookie_time? ?? ???? ???? ????? ???? ??? ????.
??? ??? ? ??? ? ????.
?? ?? ????? ?? ?? ?? ??? ? ????.
3. php_weixin_provider ??
????? config/app.php ???? ??? ?? ??? ?? ?? ?? ??? ?? ??? ?????.
4. ?3? ??? ??? ????? ?? ??
????? app/Providers/EventServiceProvider.php? ?? ??? ?????.
laravel ?????? ????? IOC?? ??? ?? ???????. js? ????? ??? ???? ?? ??? ????. ??? ??? ????? IOC(Inversion of Control)?? ??? ????. , DI: ??? ?????? ?) ?? 3??? 4???? ??? ??? ???? ? ?????.
5. WeChat ???? ?? ????? ??
?? ??? ????:
???Socialite::with('weixin')? ??? ?? php_weixin_provider ????? ?????.
拿到這個(gè)實(shí)例之后,就可以采用鏈?zhǔn)降姆绞秸{(diào)用它提供的所有public方法,比如設(shè)置配置參數(shù),setDevice等等。
6. 編寫(xiě)微信登錄回調(diào)的接口
舉例如下:
//登錄回調(diào) Route::get('/login/notify', function () { $user = null; try { $user = Socialite::with('weixin')->user(); } catch(\Exception $e) { return '獲取微信用戶異常'; } return $user->nickname; });
通過(guò)Socialite::with('weixin')拿到php_weixin_provider實(shí)例后,調(diào)用user方法,就會(huì)自動(dòng)跟微信調(diào)用相關(guān)接口,并把微信的返回值封裝成對(duì)象返回。如果在此過(guò)程中,有任何錯(cuò)誤都會(huì)以異常的形式拋出,比如state參數(shù)校驗(yàn)失敗,比如code失效等。
返回的$user對(duì)象包含的有效屬性有:
小結(jié):
這個(gè)方案是基于laravel/socialite實(shí)現(xiàn),并發(fā)布到composer來(lái)使用的。laravel/socialite是laravel官方提供的第三方登錄的模塊,基于它可以很方便的集成大部分第三方平臺(tái)的認(rèn)證,目前它官方已經(jīng)提供很多第三方的登錄實(shí)現(xiàn):https://socialiteproviders.github.io/。除了國(guó)外的facebook,google,github等,國(guó)內(nèi)的微信,微博,qq也都有提供。我在一開(kāi)始也用的是它官方提供的默認(rèn)的微信登錄provider來(lái)做的,但是后來(lái)我發(fā)現(xiàn)了以下幾個(gè)問(wèn)題:
1. 不支持微信授權(quán)的代理;
2. pc端跟移動(dòng)端竟然還是分兩個(gè)項(xiàng)目來(lái)做的:?
3. 它封裝的user對(duì)象里竟然不包含unionid
4. 更改配置參數(shù)的方式,實(shí)在是讓人覺(jué)得難以使用:?
所以我就在它官方的微信登錄provider基礎(chǔ)上,按照自己的想法,重新實(shí)現(xiàn)了一個(gè)來(lái)解決我發(fā)現(xiàn)的這些問(wèn)題。
更多WeChat ???? PHP? laravel ?????? ???? ???? ??相關(guān)文章請(qǐng)關(guān)注PHP中文網(wǎng)!

? AI ??

Undress AI Tool
??? ???? ??

Undresser.AI Undress
???? ?? ??? ??? ?? AI ?? ?

AI Clothes Remover
???? ?? ???? ??? AI ?????.

Stock Market GPT
? ??? ??? ?? AI ?? ?? ??

?? ??

??? ??

???++7.3.1
???? ?? ?? ?? ???

SublimeText3 ??? ??
??? ??, ???? ?? ????.

???? 13.0.1 ???
??? PHP ?? ?? ??

???? CS6
??? ? ?? ??

SublimeText3 Mac ??
? ??? ?? ?? ?????(SublimeText3)

usefilter_var () tovalidateemailsyntaxandcheckdnsrr () toverifydomainmxrecords.example : $ email = "user@example.com"; if (f ilter_var ($ ???, filter_validate_email) && checkdnsrr (Explode ( '@', $ email) [1], 'mx')) {echo "validandDeliverableEmail & qu

? ????? ??? ?? ???? MySQL?? ??? ??? ???? ?? ??? ??? ?? ? ?? ??? ?? ??? ?????. ?? ?? ??? ??? ?? ??? ?? (? : "END"? "CANCEL"? ?? ? ?? ? ??? ?? ???? ???? ??? ??? ??? ??? ???? ??? ???? ?? ??? ??? ???? ??? ?????. ????? ? ??? ?? ?? ??? ??? ???? ?? ??? ??? ? ?? ?? ? Coalesce? ???? ?????.

AseUnserialize (Serialize ($ obj))? AllDataisserializable ??? ?? FordeepCopying; ??? ???, ubstract__clone () tomanuallyduplicateNestEdObjectSandavoidshartReferences.

USEARRAY_MERGE () TOCOMBINEARRAYS, DUCRITINGDUPLICATESTRINGKEYSANDENTEXINGUMERICEYS; FORSIMPLERCONCATENATION, ?? ??? 55.6, USETHESPLATOPERATOR [... $ array1, ... $ array2].

?? ???? ? ?? ???? inphorganizecodecodecodeandnamingnamingconflictsbygroupingclasses, ?????, ??, andconstantsOnspecificname.2.defineanamesUsUsingThenamesPaceyWordAtTHETOPOFOFILE, AFFORBINSPACENAME, suchATESKEYSTOI

toupdateadaBasereCordInphp, FirstConnectusingpdoorMysqli, whenEseprepredStatementStoExecuteAcureCuresqlupDateQuery.example : $ pdo = newpdo ( "mysql : host = localhost; dbname = your_database", $ username, $ username, $ sql = "squer erestemail);

The__call ()? MethodsibleorundorundeRunded?? ?? ? ? MethodStrigged? ????, themodnameandarguments, asshowningwhendingderdefinedmethodslikesayhello ()

useathinfo ($ filename, pathinfo_extension) togetThefileExtension; itreliablyHandleSmultipledOtsededGecases, returningTheextension (? : "pdf") oranEmptyStringifnoneExists.
