微信官方文檔:網頁受權獲取用戶基本信息
php
具體而言,網頁受權流程分爲四步:
一、引導用戶進入受權頁面贊成受權,獲取code
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_redirecthtml
二、經過code換取網頁受權access_token(與基礎支持中的access_token不一樣)
https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_codeweb
三、若是須要,開發者能夠刷新網頁受權access_token,避免過時
https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=APPID&grant_type=refresh_token&refresh_token=REFRESH_TOKENjson
四、經過網頁受權access_token和openid獲取用戶基本信息(支持UnionID機制)
https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN //返回值無subscribe,就是不知道是否關注了微信號。網頁受權獲取用戶信息,官方用的這個方法。api
https://api.weixin.qq.com/cgi-bin/user/info?access_token=$access_token&openid=$openid&lang=zh_CN //這樣返回值有subscribe,可是官方沒提這個方法。因此這裏面的 access_token是否是網頁受權的access_token?我不確準微信
總之,這兩個接口都是能獲取用戶信息。session
但不清楚的是紅色接口,是否會致使 基礎access_token 每日2000上限的問題。不知道怎麼去驗證???有經驗的朋友能夠留言點評,謝謝。app
關於第四步:拉取用戶信息(需scope爲 snsapi_userinfo)url
若是網頁受權做用域爲snsapi_userinfo,則此時開發者能夠經過access_token和openid拉取用戶信息了。
請求方法
http:GET(請使用https協議)
https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN
返回說明spa
正確時返回的JSON數據包以下:
{
"openid":" OPENID",
" nickname": NICKNAME,
"sex":"1",
"province":"PROVINCE"
"city":"CITY",
"country":"COUNTRY",
"headimgurl": "http://wx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ4eMsv84eavHiaiceqxibJxCfHe/46",
"privilege":[
"PRIVILEGE1"
"PRIVILEGE2"
],
"unionid": "o6_bmasdasdsad6_2sgVt7hMZOPfL"
}
說明:https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN 這個方法,獲取的用戶數據,沒有關注信息(即 [subscribe] => 1)。
而$get_user_info_url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=$access_token&openid=$openid&lang=zh_CN";//這樣調用就有subscribe。
分享是一種美德,請閱代碼!
$url ="https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appid&redirect_uri=$redirect_uri&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect";
header("Location:".$url);
//返回內容: [subscribe] => 1 [openid] => oZE7SjqjC_A88aGfaj_X8tJDC [nickname] => 大仙 [sex] => 1 [language] => zh_CN [city] => 大興 [province] => 北京 [country] => 中國 [headimgurl] function getUserInfoAll($code) { $appid = WX_APP_ID; $secret = WX_SECRET; //第一步:取全局access_token $token = $global_token; //第二步:取得openid $oauth2Url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$secret&code=$code&grant_type=authorization_code"; $oauth2 = getJson($oauth2Url); //第三步:根據全局access_token和openid查詢用戶信息 $access_token = $token; $openid = $oauth2['openid']; $get_user_info_url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=$access_token&openid=$openid&lang=zh_CN";//有subscribe $userinfo = getJson($get_user_info_url); session_start(); // session_id($userinfo['openid']); if($userinfo['subscribe']==1){ $_SESSION['userinfo']=$userinfo; } return $userinfo; } //返回無subscribe function getUserInfo($code) { $appid = WX_APP_ID; $appsecret = WX_SECRET; $access_token = ""; //根據code得到Access Token $access_token_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$appsecret&code=$code&grant_type=authorization_code"; $access_token_json = https_request($access_token_url); $access_token_array = json_decode($access_token_json, true); $access_token = $access_token_array['access_token']; $openid = $access_token_array['openid']; //根據Access Token和OpenID得到用戶信息 $userinfo_url = "https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openid ";//無subscribe $userinfo = https_request($userinfo_url); $userinfo = json_decode($userinfo, true); return $userinfo; }