The WeChat public number authorized to obtain user information is divided into three parts:
1: users agree to authorize and obtain code
Jump to wechat authorization page and get the code value returned by authorization
https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect
get Parameter Description:
Appid: the only sign of WeChat public number appid
Redirect uuri: authorization callback address
Response "type: return type, fill in code
scope: authorization method: snsapi ﹣ base: silent authorization (do not pop up the authorization page, jump directly, only get the user openid), snsapi ﹣ userinfo: get the user's details
state: parameter with callback
wechat_redirect: this parameter must be taken when you directly open or do page 302 redirection
2: exchange code for web access authorization
Call to get access_token interface
https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code
get Parameter Description:
Appid: the only sign of WeChat public number appid
secret: WeChat public number appsecret
Code: code value returned by wechat authorization
Grant? Type: fixed fill in: authorization? Code
Call the above interface to return data as follows:
{ "access_token":"ACCESS_TOKEN", "expires_in":7200, "refresh_token":"REFRESH_TOKEN", "openid":"OPENID", "scope":"SCOPE" }
If you're using silent authorization, that's fine
3: get user information
Access user information interface
https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN
get Parameter Description:
Access UU token: access UU token obtained by the above interface
Openid: the openid obtained by the above interface, that is, the unique ID of the user
lang: returns the language version of the country or region, Zh CN simplified, Zh TW traditional, en English
Call the above interface to return data as follows:
{ "openid":" OPENID", " nickname": NICKNAME, "sex":"1", "province":"PROVINCE" "city":"CITY", "country":"COUNTRY", "headimgurl": "http://thirdwx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ4eMsv84eavHiaiceqxibJxCfHe/46", "privilege":[ "PRIVILEGE1" "PRIVILEGE2" ], "unionid": "o6_bmasdasdsad6_2sgVt7hMZOPfL" }
According to the above steps, the native php time code is as follows:
public function actionGetMember() { //Open session session_start(); if (!isset($_SESSION['member'])) { //appid $appId = 'wx73d0c47a64aa5315'; //secret $appSecret = 'aba2793c10623350f6aeee5a728099d3'; if (!isset($_GET['code'])) { //To grant authorization $this->authorize($appId); } else { $code = $_GET['code']; //Get access_token and openID $res = $this->getAccessToken($appId, $appSecret, $code); $accessToken = $res['access_token']; $openId = $res['openid']; //Get user information $this->getMember($accessToken, $openId); } } $member = isset($_SESSION['member']) ? $_SESSION['member'] : []; var_dump($member); } /* * Get user information */ public function getMember($accessToken, $openId) { $params = []; $params['access_token'] = $accessToken; $params['openid'] = $openId; $params['lang'] = 'zh_CN'; $urlParams = $this->urlParams($params); $memberUrl = 'https://api.weixin.qq.com/sns/userinfo?' . $urlParams; $member = $this->http_curl($memberUrl); $member = json_decode($member, true); $_SESSION['member'] = $member; } /* * To grant authorization */ public function authorize($appId) { //Get current url $redirectUrl = $this->getUrl(); $params = []; $params['appid'] = $appId; $params['redirect_uri'] = $redirectUrl; $params['response_type'] = 'code'; $params['scope'] = 'snsapi_userinfo'; $params['state'] = 'STATE'; $urlParams = $this->urlParams($params); $url = 'https://open.weixin.qq.com/connect/oauth2/authorize?'. $urlParams .'#wechat_redirect'; header('location:' . $url); } /* * String splicing */ public function urlParams($params) { $options = ''; foreach ($params as $key => $value) { $options .= $key . '=' . $value .'&'; } $options = rtrim($options, '&'); return $options; } /* * Get current url */ public function getUrl() { //Get agreement type $protocalPort = isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == '443' ? 'https://' : 'http://'; //Get the url of the currently executed script $phpSelf = $_SERVER['PHP_SELF'] ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME']; $pathInfo = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : ''; $queryString = isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : ''; $relateUrl = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : $phpSelf . (!empty($queryString) ? '?' . $queryString : $pathInfo); $url = $protocalPort . (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '') . $relateUrl; return $url; } /* * Get access_token */ public function getAccessToken($appId, $appSecret, $code) { $params = []; $params['appid'] = $appId; $params['secret'] = $appSecret; $params['code'] = $code; $params['grant_type'] = 'authorization_code'; $urlParams = $this->urlParams($params); $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?' . $urlParams; $result = $this->http_curl($url); $result = json_decode($result, true); return $result; } /* * curl Interface call */ public function http_curl($url, $data=null) { $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); $result = curl_exec($curl); curl_close($curl); return $result; }