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

微信开放平台提供了网站扫码登录的接口,用于获取用户基本信息(头像,昵称)方便网站快速接入微信登录,快捷登录。需要使用登录接口,需要成为微信开放平台认证开发者(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

参数说明

 

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

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>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值