要使用微信授权登录功能需要先在微信开发平台创建应用。然后会获取微信提供给你的appId和AppSecret,然后就可以进行开发了。
当然现有很多大佬封装的微信类库非常齐全,而且还很好用,可以去试试,下面讲解一下基本实现方法。
流程
- 用户同意授权后获取code,code有效期10分钟
- 使用code获取
access_token调用接口凭证,有效期2小时refresh_token当access_token过期可以使用这个进行刷新,有效期30天openid普通用户的标识 - 刷新token
- 通过token和openid获取用户信息
若access_token已超时,那么进行refresh_token会获取一个新的access_token,新的超时时间。若access_token未超时,那么进行refresh_token不会改变access_token,但超时时间会刷新,相当于续期access_token。
refresh_token拥有较长的有效期(30天),当refresh_token失效的后,需要用户重新授权。
获取用户信息
移动端开发由移动端获取code,网页开发用php获取就可以。下面是一个简单的移动端获取用户信息的方法,使用第二步和第四步就可以了。
public function get_user_info($code) {// 通过code获取access_token$get_token_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" . $this->appid . "&secret=" . $this->appsecret . "&code={$code}&grant_type=authorization_code";$token_info = $this->https_request($get_token_url);$token_info = json_decode($token_info, true);if (isset($token_info['errcode'])) {$this->errCode = $token_info['errcode'];$this->errMsg = $token_info['errmsg'];return false;}// 通过access_token和openid获取用户信息$get_userinfo_url = 'https://api.weixin.qq.com/sns/userinfo?access_token=' . $token_info['access_token'] . '&openid=' . $token_info['openid'] . '&lang=zh_CN';$userinfo = $this->https_request($get_userinfo_url);$userinfo = json_decode($userinfo, true);if (isset($userinfo['errcode'])) {$this->errCode = $userinfo['errcode'];$this->errMsg = $userinfo['errmsg'];return false;}return $userinfo;}
- 封装成公共类如下,(使用方法)
<?php/*** 微信授权登录获取用户信息* @param $appid 微信应用appid* @param $appsecret 微信应用appsecret* @author codehui <admin@codehui.net> 2018-3-26*/class WxOauth{private $appid = ""; // appidprivate $appsecret = ""; // appsecretpublic $error = []; // 错误信息const GET_ACCESS_TOKEN_URL = 'https://api.weixin.qq.com/sns/oauth2/access_token'; // 获取access_token urlconst GET_USER_INFO_URL = 'https://api.weixin.qq.com/sns/userinfo'; // 获取用户信息urlconst GET_REFRESH_URL = 'https://api.weixin.qq.com/sns/oauth2/refresh_token'; //刷新access_tokenconst GET_CODE = 'https://open.weixin.qq.com/connect/oauth2/authorize'; // 获取code(网页授权使用)public function __construct($appid, $appsecret) {if($appid && $appsecret){$this->appid = $appid;$this->appsecret = $appsecret;}}/*** 微信登录* @param string $code 客户端传回的code(网页授权时调用getCode方法获取code,微信会把code返回给redirect_uri)* @return array 用户信息* @example 错误时微信会返回错误码等信息 eg:{"errcode":, "errmsg":""}*/public function wxLogin($code){$token_info = $this->getToken($code);if (isset($token_info['errcode'])) {$this->error = $token_info;return false;}$user_info = $this->getUserinfo($token_info['openid'], $token_info['access_token']);if (isset($user_info['errcode'])) {$this->error = $user_info;return false;}return $user_info;}/*** 用户同意授权获取code* @param string $redirect_uri 授权后重定向的回调链接地址,需要urlEncode处理* @return redirect*/public function getCode($redirect_uri){$uri = $this->combineURL(self::GET_CODE, ['appid' => $this->appid,'scope' => 'SCOPE','response_type' => 'code','redirect_uri' => urlEncode($redirect_uri),'state' => 'STATE#wechat_redirect',]);header('Location: ' . $uri, true);}/*** 获取token和openid* @param string $code 客户端传回的code* @return array 获取到的数据*/public function getToken($code){$get_token_url = $this->combineURL(self::GET_ACCESS_TOKEN_URL, ['appid' => $this->appid,'appsecret' => $this->appsecret,'code' => $code,'grant_type' => 'authorization_code']);$token_info = $this->httpsRequest($get_token_url);return json_decode($token_info, true);}/*** 刷新access token并续期* @param string $refresh_token 用户刷新access_token* @return array*/public function refreshToken($refresh_token){$refresh_token_url = $this->combineURL(self::GET_REFRESH_URL, ['appid' => $this->appid,'refresh_token' => $refresh_token,'grant_type' => 'refresh_token']);$refresh_info = $this->httpsRequest($refresh_token_url);return json_decode($refresh_info, true);}/*** 获取用户信息* @param string $openid 用户的标识* @param string $access_token 调用接口凭证* @return array 用户信息*/public function getUserinfo($openid, $access_token){$get_userinfo_url = $this->combineURL(self::GET_USER_INFO_URL, ['openid' => $openid,'access_token' => $access_token,'lang' => 'zh_CN']);$user_info = $this->httpsRequest($get_userinfo_url);return json_decode($user_info, true);}/*** 拼接url* @param string $baseURL 请求的url* @param array $keysArr 参数列表数组* @return string 返回拼接的url*/public function combineURL($baseURL, $keysArr){$combined = $baseURL . "?";$valueArr = array();foreach($keysArr as $key => $val){$valueArr[] = "$key=$val";}$keyStr = implode("&", $valueArr);$combined .= ($keyStr);return $combined;}/*** 获取服务器数据* @param string $url 请求的url* @return unknown 请求返回的内容*/public function httpsRequest($url) {$curl = curl_init();curl_setopt($curl, CURLOPT_URL, $url);curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);$output = curl_exec($curl);curl_close($curl);return $output;}}
使用方法
// 移动端使用$WxOauth = new WxOauth(APPID, APPSECRET); // 传入appid和appsecret//公众号登录需要先获取code,下面方法会自动跳转到微信授权页面$WxOauth->getCode();// 通过移动端传来的code或者微信回调返回的code获取用户信息$user_info = $WxOauth->wxLogin($_REQUEST['code']);if($user_info){//获取到用户信息}else{// 获取错误$WxOauth->error;}
