您的位置:首页 > 移动开发 > 微信开发

php微信授权登录实例讲解

2021-04-15 04:06 781 查看

要使用微信授权登录功能需要先在微信开发平台创建应用。然后会获取微信提供给你的appIdAppSecret,然后就可以进行开发了。
当然现有很多大佬封装的微信类库非常齐全,而且还很好用,可以去试试,下面讲解一下基本实现方法。

流程

  • 用户同意授权后获取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获取就可以。下面是一个简单的移动端获取用户信息的方法,使用第二步和第四步就可以了。

publicfunction 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'];
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
/**
* 微信授权登录获取用户信息
* @param $appid 微信应用appid
* @param $appsecret 微信应用appsecret
* @author codehui <admin@codehui.net> 2018-3-26
*/
classWxOauth
{
private $appid ="";// appid
private $appsecret ="";// appsecret
public $error =[];// 错误信息
const GET_ACCESS_TOKEN_URL ='https://api.weixin.qq.com/sns/oauth2/access_token';// 获取access_token url
const GET_USER_INFO_URL ='https://api.weixin.qq.com/sns/userinfo';// 获取用户信息url
const GET_REFRESH_URL ='https://api.weixin.qq.com/sns/oauth2/refresh_token';//刷新access_token
const GET_CODE ='https://open.weixin.qq.com/connect/oauth2/authorize';// 获取code(网页授权使用)
publicfunction __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":""}
*/
publicfunction wxLogin($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
* @param string $redirect_uri 授权后重定向的回调链接地址,需要urlEncode处理
* @return redirect
*/
publicfunction 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 获取到的数据
*/
publicfunction 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
*/
publicfunction 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 用户信息
*/
publicfunction 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
*/
publicfunction 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 请求返回的内容
*/
publicfunction 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 =newWxOauth(APPID, APPSECRET);// 传入appid和appsecret
//公众号登录需要先获取code,下面方法会自动跳转到微信授权页面
$WxOauth->getCode();
// 通过移动端传来的code或者微信回调返回的code获取用户信息
$user_info = $WxOauth->wxLogin($_REQUEST['code']);
if($user_info){
//获取到用户信息
}else{
// 获取错误
$WxOauth->error;
}

到此这篇关于php微信授权登录实例讲解的文章就介绍到这了,更多相关php微信授权登录内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  php 微信登录