WeChat ?? ??? ?? ? ??? ??? ?? ? ?? ???? ??? ?? ??? ??? ??????. ? ? ???? ??? ?? ?? ???? ???? ?? ?????. ? ????? ThinkPHP5 ?????? ???? WeChat ?? ?? ? ???? ???? ??? ?????.
1. ?? ?? ?? ? AppID ? AppSecret ??
WeChat ?? ?? ????? ???? ?? ?? WeChat ?? ??? ???? ??? ??? ???? ???. ?? ????? ????? ???? "??? ??"?? ? ?? ??? ????? AppID? AppSecret? ?? ? ????.
2. ?? ?? ?? ??
ThinkPHP5 ???????? config ????? ??? wechat.php ??? ???? ?? ?? ?? ??? ??? ? ????. ? ???? ?? ??? ???? ???.
<?php return [ 'app_id' =>?'your?appid', ????'app_secret'?=>?'your?appsecret', ????'auth_redirect'?=>?'your?callback?url', ];
? ?:
-
app_id
?app_secret
? ?? ?? ?? ??????? ?? ???????. -
auth_redirect
是微信網(wǎng)頁授權(quán)后的回調(diào)地址,要求必須是公網(wǎng)可訪問的URL地址。
app_id
和app_secret
是我們?cè)诠娞?hào)管理界面獲得的參數(shù)。三、獲取網(wǎng)頁授權(quán)url
在我們調(diào)用微信網(wǎng)頁授權(quán)接口前,需要構(gòu)造網(wǎng)頁授權(quán)url。我們可以在控制器中添加以下代碼:
$config?=?config('wechat'); $url?=?'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' ????????.?$config['app_id'] ????????.?'&redirect_uri=' ????????.?urlencode($config['auth_redirect']) ????????.?'&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect';
以上代碼中,我們通過config函數(shù)獲取到了之前配置的公眾號(hào)信息,并構(gòu)造了一個(gè)微信網(wǎng)頁授權(quán)的URI。其中,response_type=code
表示使用code方式進(jìn)行授權(quán),scope=snsapi_userinfo
表示授權(quán)范圍為獲取用戶基本信息。
四、獲取網(wǎng)頁授權(quán)code
構(gòu)造好網(wǎng)頁授權(quán)的URL后,我們需要跳轉(zhuǎn)該URL,進(jìn)行授權(quán)。授權(quán)成功后,微信服務(wù)器會(huì)將code參數(shù)通過GET方式傳遞回來。我們可以在控制器中添加以下代碼,用于獲取code。
if?(isset($_GET['code']))?{ ????$code?=?$_GET['code']; }?else?{ ????$this->redirect($url); }
以上代碼中,我們首先判斷URL中是否包含了code參數(shù)。如果有,則表示用戶已經(jīng)授權(quán)成功,我們將獲取到的code存儲(chǔ)起來,以便后續(xù)使用。如果沒有,則需要進(jìn)行跳轉(zhuǎn),進(jìn)行網(wǎng)頁授權(quán)。
五、獲取用戶access_token和openId
在授權(quán)成功后,后續(xù)的操作需要用到access_token和openId。我們可以在控制器中添加以下代碼,用于獲取用戶的access_token和openId。
$accessTokenUrl?=?'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' ????????????????????.?$config['app_id'] ????????????????????.?'&secret=' ????????????????????.?$config['app_secret'] ????????????????????.?'&code=' ????????????????????.?$code ????????????????????.?'&grant_type=authorization_code'; $accessTokenResponse?=?json_decode(file_get_contents($accessTokenUrl),?true); if?(isset($accessTokenResponse['errcode']))?{ ????throw?new?\Exception('ERROR?'?.?$accessTokenResponse['errcode']?.?':?'?.?$accessTokenResponse['errmsg']); } $accessToken?=?$accessTokenResponse['access_token']; $openId?=?$accessTokenResponse['openid'];
以上代碼中,我們首先構(gòu)造了一個(gè)請(qǐng)求access_token的URL,并向該URL發(fā)送了請(qǐng)求,獲取到了響應(yīng)結(jié)果。如果響應(yīng)結(jié)果中包含了errcode
,則表示請(qǐng)求出現(xiàn)了錯(cuò)誤,我們將拋出一個(gè)異常;否則,我們將獲取到的access_token和openId存儲(chǔ)起來,以便后續(xù)使用。
六、獲取用戶詳細(xì)信息
在獲取到用戶的access_token和openId后,我們可以通過以下代碼,獲取到用戶的詳細(xì)信息:
$userInfoUrl?=?'https://api.weixin.qq.com/sns/userinfo?access_token=' ????????????????.?$accessToken ????????????????.?'&openid=' ????????????????.?$openId ????????????????.?'&lang=zh_CN'; $userInfoResponse?=?json_decode(file_get_contents($userInfoUrl),?true); if?(isset($userInfoResponse['errcode']))?{ ????throw?new?\Exception('ERROR?'?.?$userInfoResponse['errcode']?.?':?'?.?$userInfoResponse['errmsg']); }
以上代碼中,我們構(gòu)造了一個(gè)請(qǐng)求用戶信息的URL,并向該URL發(fā)送了請(qǐng)求,獲取到了響應(yīng)結(jié)果。如果響應(yīng)結(jié)果中包含了errcode
auth_redirect
? WeChat ???? ?? ?? ?? ?????. ?? ?????? ???? ? ?? URL ???? ???.
response_type=code
? ??? ?? ?? ??? ????? ????, scope=snsapi_userinfo
? ?? ??? ?? ??? ??? ?? ??? ?????. ????4. ???? ?? ?? ?????????? ?? URL? ??? ? ??? ?? ?? URL? ???? ???. ??? ???? WeChat ??? GET? ?? ?? ????? ?? ?????. ????? ?? ??? ???? ??? ?? ? ????. ??rrreee??? ????? ?? URL? ?? ????? ???? ??? ?????. ?? ?? ?? ???? ????? ?????? ???? ??? ?? ??? ?? ??? ??? ?????. ??? ?? ?? ???? ???? ??? ???? ???. ????5. ??? access_token ? openId??????? ????? ???? ?? ???? access_token ? openId? ???????. ???? access_token ? openId? ?? ?? ????? ?? ??? ??? ? ????. ??rrreee??? ????? ?? access_token? ???? URL? ???? ?? URL? ??? ??? ?? ??? ?????. ?? ??? errcode
? ???? ??? ??? ??? ??? ???? ??? ???? ?? ??? ?? ??? access_token ? openId? ?????. ????6. ??? ?? ?? ?????????? access_token ? openId? ?? ? ?? ??? ?? ???? ?? ??? ?? ? ????. ??rrreee??? ????? ??? ??? ???? URL? ???? ??? ???????. URL? ???? ?? ??? ?????. ?? ??? errcode
? ???? ??? ??? ??? ??? ???? ??? ?????. ?????? WeChat ?? ?? ???? ?? ??? ????? ???????! ??? ??? ThinkPHP5 ?????? WeChat ?? ?? ? ???? ??? ??????? ?? ?????. ??? ??? PHP ??? ????? ?? ?? ??? ?????!

? AI ??

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

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

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

Clothoff.io
AI ? ???

Video Face Swap
??? ??? AI ?? ?? ??? ???? ?? ???? ??? ?? ????!

?? ??

??? ??

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

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

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

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

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