微信網(wǎng)頁授權(quán)文檔地址 : https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140842
2 第二步:通過code換取網(wǎng)頁授權(quán)access_token
4 第四步:拉取用戶信息(需scope為 snsapi_userinfo)
<?php //獲取code $code = $_GET['code']; // 在微信公眾號(hào)請(qǐng)求用戶網(wǎng)頁授權(quán)之前,開發(fā)者需要先到公眾平臺(tái)官網(wǎng)中的“開發(fā) - 接口權(quán)限 - 網(wǎng)頁服務(wù) - 網(wǎng)頁帳號(hào) - 網(wǎng)頁授權(quán)獲取用戶基本信息”的配置選項(xiàng)中,修改授權(quán)回調(diào)域名。請(qǐng)注意,這里填寫的是完整域名(是一個(gè)字符串),而不是URL,因此請(qǐng)勿加 http:// 等協(xié)議頭; $appid = 'your appid'; // 微信公眾平臺(tái)申請(qǐng)的APPID $secret = 'your secret'; // 微信公眾平臺(tái)申請(qǐng)的AppSecret // 前端訪問以下地址進(jìn)行授權(quán)登陸, 例子: https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxf0e81c3bee622d60&redirect_uri=http%3A%2F%2Fnba.bluewebgame.com%2Foauth_response.php&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect //獲取access_token $token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$secret.'&code='.$code.'&grant_type=authorization_code'; $token_res = https_request($token_url); $token_data = json_decode($token_res,true); $token = $token_data['access_token']; $openid = $token_data['openid']; //獲取用戶信息 $userinfo_url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$token.'&openid='.$openid.'&lang=zh_CN'; $userinfo_res = https_request( $userinfo_url); //轉(zhuǎn)換用戶信息 $userinfo_data = json_decode($userinfo_res,true); if (empty($userinfo_data['unionid'])) { echo '授權(quán)失敗, 請(qǐng)重試'; } else { // 數(shù)據(jù)處理 $data['nickname'] = $userinfo_data['nickname']; // 用戶昵稱 $data['sex'] = $userinfo_data['sex']; // 用戶性別,1為男性,2為女性 $data['province'] = $userinfo_data['province']; // 用戶個(gè)人資料填寫的省份 $data['city'] = $userinfo_data['city']; // 用戶個(gè)人資料填寫的城市 $data['country'] = $userinfo_data['country']; // 國家,如中國為CN $data['headimgurl'] = $userinfo_data['headimgurl']; // 用戶頭像,最后一個(gè)數(shù)值代表正方形頭像大?。ㄓ?、46、64、96、132數(shù)值可選,0代表640*640正方形頭像),用戶沒有頭像時(shí)該項(xiàng)為空 $data['unionid'] = $userinfo_data['unionid']; // 用戶統(tǒng)一標(biāo)識(shí)。針對(duì)一個(gè)微信開放平臺(tái)帳號(hào)下的應(yīng)用,同一用戶的unionid是唯一的。 $data['openid'] = $userinfo_data['openid']; // 用戶唯一標(biāo)識(shí)。 echo '授權(quán)登陸成功'; }
浙公網(wǎng)安備 33010602011771號(hào)