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

zonehh 2019-06-30

微信开放平台提供了网站扫码登录的接口,用于获取用户基本信息(头像,昵称)方便网站快速接入微信登录,快捷登录。需要使用登录接口,需要成为微信开放平台认证开发者(300元)才可以获得这个接口权限。

准备工作:

1、准备APPID、APPSECRET
2、准备接口地址
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、获取CODE
2、获取access_token、openid
3、获取用户信息

操作:

1、请求CODE

参数说明

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

通过接口地址,拼接以上参数进行访问即可

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>";
?>

然后就跳转到了一个扫码的页面了:

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

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:点击查看

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

相关推荐