要使用微信授权登录功能需要先在微信开发平台创建应用。然后会获取微信提供给你的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获取就可以,下面是一个简单的移动端获取用户信息的方法,使用第二步和第四步就可以了。
publicfunctionget_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'];
returnfalse;
}
//通过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'];
returnfalse;
}
return$userinfo;
}
封装成公共类如下
<?php
/**
*微信授权登录获取用户信息
[email protected]$appid微信应用appid
[email protected]$appsecret微信应用appsecret
[email protected]<[email protected]>2018-3-26
*/
classWxOauth
{
private$appid="";//appid
private$appsecret="";//appsecret
public$error=[];//错误信息
constGET_ACCESS_TOKEN_URL='https://api.weixin.qq.com/sns/oauth2/access_token';//获取access_tokenurl
constGET_USER_INFO_URL='https://api.weixin.qq.com/sns/userinfo';//获取用户信息url
constGET_REFRESH_URL='https://api.weixin.qq.com/sns/oauth2/refresh_token';//刷新access_token
constGET_CODE='https://open.weixin.qq.com/connect/oauth2/authorize';//获取code(网页授权使用)
publicfunction__construct($appid,$appsecret){
if($appid&&$appsecret){
$this->appid=$appid;
$this->appsecret=$appsecret;
}
}
/**
*微信登录
[email protected]$code客户端传回的code(网页授权时调用getCode方法获取code,微信会把code返回给redirect_uri)
*@returnarray用户信息
[email protected]错误时微信会返回错误码等信息eg:{"errcode":,"errmsg":""}
*/
publicfunctionwxLogin($code){
$token_info=$this->getToken($code);
if(isset($token_info['errcode'])){
$this->error=$token_info;
returnfalse;
}
$user_info=$this->getUserinfo($token_info['openid'],$token_info['access_token']);
if(isset($user_info['errcode'])){
$this->error=$user_info;
returnfalse;
}
return$user_info;
}
/**
*用户同意授权获取code
[email protected]$redirect_uri授权后重定向的回调链接地址,需要urlEncode处理
[email protected]
*/
publicfunctiongetCode($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
[email protected]$code客户端传回的code
[email protected]获取到的数据
*/
publicfunctiongetToken($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);
returnjson_decode($token_info,true);
}
/**
*刷新accesstoken并续期
[email protected]$refresh_token用户刷新access_token
[email protected]
*/
publicfunctionrefreshToken($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);
returnjson_decode($refresh_info,true);
}
/**
*获取用户信息
[email protected]$openid用户的标识
[email protected]$access_token调用接口凭证
*@returnarray用户信息
*/
publicfunctiongetUserinfo($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);
returnjson_decode($user_info,true);
}
/**
*拼接url
[email protected]$baseURL请求的url
[email protected]$keysArr参数列表数组
[email protected]返回拼接的url
*/
publicfunctioncombineURL($baseURL,$keysArr){
$combined=$baseURL."?";
$valueArr=array();
foreach($keysArras$key=>$val){
$valueArr[]="$key=$val";
}
$keyStr=implode("&",$valueArr);
$combined.=($keyStr);
return$combined;
}
/**
*获取服务器数据
[email protected]$url请求的url
*@returnunknown请求返回的内容
*/
publicfunctionhttpsRequest($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=newWxOauth(APPID,APPSECRET);//传入appid和appsecret
//公众号登录需要先获取code,下面方法会自动跳转到微信授权页面
$WxOauth->getCode();
//通过移动端传来的code或者微信回调返回的code获取用户信息
$user_info=$WxOauth->wxLogin($_REQUEST['code']);
if($user_info){
//获取到用户信息
}else{
//获取错误
$WxOauth->error;
}
|