微信公众号授权登陆
版权声明:
本文为博主原创文章,转载请声明原文链接...谢谢。o_0。
更新时间:
2017-01-13 22:56:55
温馨提示:
学无止境,技术类文章有它的时效性,请留意文章更新时间,如发现内容有误请留言指出,防止别人"踩坑",我会及时更新文章
官方文档地址
https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140842&token=&lang=zh_CN
首先确保已经取得啦微信的 APPID 和 APPSECRET
使用的步骤如下
1 第一步:用户同意授权,获取code
2 第二步:通过code换取网页授权access_token
3 第三步:刷新access_token(如果需要)
4 第四步:拉取用户信息(需scope为 snsapi_userinfo)
首先取code
在确保微信公众账号拥有授权作用域(scope参数)的权限的前提下(服务号获得高级接口后,默认拥有scope参数中的snsapi_base和snsapi_userinfo)
/**
* 微信公众号授权登陆
* @var string
*/
$appid = "xxxxxxxxxxxxxxxxxx";
$appkey = "xxxxxxxxxxxxxxxxxxxxxx";
$redirect_uri = urlencode("http://www.xxxxxxxx.com/oauth2.php?". $_SERVER['QUERY_STRING']);
if (isset($_GET['code'])) {
//如果有code参数
$code = $_GET['code'];
echo $code;
} else {
//没有code的情况,//通过这个url取跳转到授权页面取code值
$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}"); //跳转
}打开上面文件后会出现授权页面

同意授权后就会输出code的值 然后就可以使用code去取access_token啦
//如果有code参数
$code = $_GET['code'];
//使用code换取access_token
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$appid}&secret={$appkey}&code={$code}&grant_type=authorization_code";
$json = file_get_contents($url);
echo $json;
$json = json_decode($json, true);
//数据格式见网页最下面
if (isset($json['errorcode'])) {
//错误
die($json['errmsg']);
}然后就可以使用上一步取得的access_token和openid去取用户的信息
//使用access_token获取用户信息
$access_token = $json['access_token'];
$openid = $json['openid'];
$userurl = "https://api.weixin.qq.com/sns/userinfo?access_token={$access_token}&openid={$openid}";
$uinfo = file_get_contents($userurl);
echo $uinfo;
$uinfo = json_decode($uinfo, true);
if (isset($uinfo['errorcode'])) {
//错误
die($uinfo['errmsg']);
}下面提供一个完整的代码 oauth2.php
/**
* 微信公众号授权登陆
* @var string
*/
$appid = "xxxxxxxxxxxxxxxxxx";
$appkey = "xxxxxxxxxxxxxxxxxxx";
//下面回调地址需要在微信公众号后台设置权限域名和目录,注意要具体到目录
$redirect_uri = urlencode("http://www.xxxxxx.com/oauth2.php?". $_SERVER['QUERY_STRING']);
if (isset($_GET['code'])) {
//如果有code参数
$code = $_GET['code'];
// 使用code换取access_token
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$appid}&secret={$appkey}&code={$code}&grant_type=authorization_code";
$json = file_get_contents($url);
echo $json;
$json = json_decode($json, true);
if (isset($json['errorcode'])) {
//错误
die($json['errmsg']);
}
//使用access_token获取用户信息
$access_token = $json['access_token'];
$openid = $json['openid'];
$userurl = "https://api.weixin.qq.com/sns/userinfo?access_token={$access_token}&openid={$openid}";
$uinfo = file_get_contents($userurl);
echo $uinfo;
$uinfo = json_decode($uinfo, true);
if (isset($uinfo['errorcode'])) {
//错误
die($uinfo['errmsg']);
}
//数据格式见网页最下面
//标识id
$unionid = $uinfo['unionid'];
} else {
//没有code的情况,//通过这个url取跳转到授权页面取code值
$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}"); //跳转
}接口返因的数据格式
//错误数据
{"errcode":40029,"errmsg":"invalid code"}
//access_token返回的数据
{
"access_token": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"expires_in": 7200,
"refresh_token": "6ZnsZaCPUhbA1Lu-086pdWFVNIjSfCFVACZcubm1NrN99NnllVkTho4xf2kD2ppoUS9c7XYutYSPa3qSwP7Toj13diGRzdTyxwrefp2-d0g",
"openid": "xxxxxxxxxxxxxxxxxxxxxx",
"scope": "snsapi_userinfo",
"unionid": "xxxxxxxxxxxxxxxxxxxxxx"
}
//返回的用户信息数据
{
"openid": "xxxxxxxxxxxxxxxxx",
"nickname": "_SMILE_",
"sex": 1,
"language": "zh_CN",
"city": "Zhengzhou",
"province": "Henan",
"country": "CN",
"headimgurl": "http://wx.qlogo.cn/mmopen/qUNf9NP4MdVibbq42RzciaZiaqAguImlw3DmzfvX1aawaSmmwV6hJ4NAcRhgt9mKhNmzGXN04PxB2jYG1Vhwtu1fhNuUYlP2qPW/0",
"privilege": [],
"unionid": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
}