微信开放平台扫码登录获取用户基本信息!附可用demo

31次阅读

共计 5249 个字符,预计需要花费 14 分钟才能阅读完成。

微信开放平台提供了网站扫码登录的接口,用于获取用户基本信息(头像,昵称)方便网站快速接入微信登录,快捷登录。需要使用登录接口,需要成为微信开放平台认证开发者(300 元)才可以获得这个接口权限。
准备工作:
1、准备 APPID、APPSECRET2、准备接口地址 3、准备 REDIRECT_URI
获取 code 接口
https://open.weixin.qq.com/connect/qrconnect?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect
获取 acess_token、openid 接口
https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code
获取用户信息接口:
https://api.weixin.qq.com/sns/userinfo?access_token=access_token&openid=openid
流程:
1、获取 CODE2、获取 access_token、openid3、获取用户信息
操作:
1、请求 CODE
参数说明

通过接口地址,拼接以上参数进行访问即可
https://open.weixin.qq.com/connect/qrconnect?appid=APPID&redirect_uri= 这里填写 redirect_uri&response_type=code&scope=SCOPE&state=STATE#wechat_redirect
redirect_uri 说明这是点击上面地址扫码后跳转的地址,跳转的地址回给你带上两个参数,code 和 state 参数。
state 说明用于保持请求和回调的状态,授权请求后原样带回给第三方。该参数可用于防止 csrf 攻击(跨站请求伪造攻击),建议第三方带上该参数,可设置为简单的随机数加 session 进行校验。
可以自己生成随机字符串,为了简单学习,我这里用时间戳进行 MD5 加密简单生成
<?php
$data = time();
$state = MD5($data);
?>
例如你的 redirect_uri 是 http://www.baidu.com/login.php,那么扫码后,跳转的地址会是这样的。
http://www.baidu.com/login.php?code= 生成的 code&state= 生成的 state
当然 redirect_uri 需要进行 urlEncode 编码。
<?php
$redirect_uri = urlEncode(“http://www.baidu.com/login.php”);
?>
最终获取 CODE 的访问链接就是这样的:
<?php
$appid = “ 填写你的 APPID”;
$redirect_uri = UrlEncode(“http://www.baidu.com/login.php”);
$data = time();
$state = MD5($data);
// 跳转页面
echo “<script>location.href=\”https://open.weixin.qq.com/connect/qrconnect?appid=$appid&redirect_uri=$redirect_uri&response_type=code&scope=snsapi_login&state=$state#wechat_redirect\”;</script>”;
?>
然后就跳转到了一个扫码的页面了:

2、获取 access_token 和 openid
通过 curl 向接口发起请求即可
<?php
// 从 redirect_uri 得到 code
$code = $_GET[“code”];
$appid = “ 填写你的 ”;
$secret = “ 填写你的 ”;

// 获取 access_token 和 openid
$url = “https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$secret&code=$code&grant_type=authorization_code”;
function post($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$rst = curl_exec($ch);
curl_close($ch);
return $rst;
}

// 发送请求
$result = post($url);
// 返回接口的数据
$arr = json_decode($result,true);
// 解析 json,单独把 openid 和 access_token 取出来待会用
$openid = $arr[‘openid’];
$token = $arr[‘access_token’];
?>
3、获取用户信息
<?php
// 这里是接着上面的代码的
// 获取用户信息需要 openid 和 access_token
// 获取用户信息
$getinfourl = “https://api.weixin.qq.com/sns/userinfo?access_token=$token&openid=$openid”;
function getinfo($getinfourl) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $getinfourl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$rst = curl_exec($ch);
curl_close($ch);
return $rst;
}

// 发送请求获取用户信息
$info_result = getinfo($getinfourl);
// 返回接口的数据
// echo $info_result;
$info_arr = json_decode($info_result,true);
$nickname = $info_arr[‘nickname’];
$headimgurl = $info_arr[‘headimgurl’];

// 显示头像和昵称
echo “<img src=\”$headimgurl\”/>”;
echo “<h2>$nickname<h2>”;
?>
完整代码
code.php
<?php
$appid = “ 填写你的 ”;
$redirect_uri = UrlEncode(“http://www.baidu.com/login.php”);
$data = time();
$state = MD5($data);

echo “<script>location.href=\”https://open.weixin.qq.com/connect/qrconnect?appid=$appid&redirect_uri=$redirect_uri&response_type=code&scope=snsapi_login&state=$state#wechat_redirect\”;</script>”;

?>
login.php
<!DOCTYPE html>
<html>
<head>
<title> 登录成功!</title>
<style type=”text/css”>
*{margin:0px;padding: 0px;}
#headimg{
width: 180px;
height: 180px;
margin:100px auto 10px;
border-radius: 100%;
}

#headimg img{
width: 180px;
height: 180px;
border-radius: 100%;
}

h2{
text-align: center;
}

p{
text-align: center;
font-size: 38px;
font-weight: bold;
margin-top: 20px;
}
</style>
</head>
<body>

</body>
</html>

<?php
$code = $_GET[“code”];
$appid = “ 填写你的 ”;
$secret = “ 填写你的 ”;

// 获取 access_token 和 openid
$url = “https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$secret&code=$code&grant_type=authorization_code”;
function post($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$rst = curl_exec($ch);
curl_close($ch);
return $rst;
}

// 发送请求
$result = post($url);
// 返回接口的数据
$arr = json_decode($result,true);
$openid = $arr[‘openid’];
$token = $arr[‘access_token’];

// 获取用户信息
$getinfourl = “https://api.weixin.qq.com/sns/userinfo?access_token=$token&openid=$openid”;
function getinfo($getinfourl) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $getinfourl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$rst = curl_exec($ch);
curl_close($ch);
return $rst;
}

// 发送请求获取用户信息
$info_result = getinfo($getinfourl);
// 返回接口的数据
// echo $info_result;
$info_arr = json_decode($info_result,true);
$nickname = $info_arr[‘nickname’];
$headimgurl = $info_arr[‘headimgurl’];
$errcode = $info_arr[‘errcode’];

if ($errcode == “41001”) {
echo “<p> 登录失效,请重新扫码登录 <p>”;
echo “<p><a href=\”code.php\”> 登录 </a><p>”;
}else{
echo “<div id=\”headimg\”><img src=\”$headimgurl\”/></div>”;
echo “<h2>$nickname<h2>”;
echo “<p> 登录成功 <p>”;
}
?>
DEMO:http://www.likeyunba.com/code…

时间:2018-1-26 作者:TANKING 网站:http://likeyunba.com

正文完
 0