微信登录,授权获取用户基本信息

方法一:

1、首先登录微信公众号平台,配置你的回调函数页面
这里写图片描述

这里写图片描述

2、配置js 接口安全域名(用来调用js接口)
这里写图片描述

3、在新建网站根目录新建getcodeurl.php添加代码如下

//换成自己的接口信息

$appid = 'XXXXX';

header('location:https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$appid.'&redirect_uri=你的域名/oauth.php&response_type=code&scope=snsapi_userinfo&state=123&connect_redirect=1#wechat_redirect');

这里写图片描述

4、新建oauth.php 文件 添加代码如下

<?php
$code = $_GET['code'];
$state = $_GET['state'];
//换成自己的接口信息
$appid = 'XXXXX';
$appsecret = 'XXXXX';
if (empty($code)) $this->error('授权失败');
$token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$appsecret.'&code='.$code.'&grant_type=authorization_code';
$token = json_decode(file_get_contents($token_url));
if (isset($token->errcode)) {
    echo '<h1>错误:</h1>'.$token->errcode;
    echo '<br/><h2>错误信息:</h2>'.$token->errmsg;
    exit;
}
$access_token_url = 'https://api.weixin.qq.com/sns/oauth2/refresh_token?appid='.$appid.'&grant_type=refresh_token&refresh_token='.$token->refresh_token;
//转成对象
$access_token = json_decode(file_get_contents($access_token_url));
if (isset($access_token->errcode)) {
    echo '<h1>错误:</h1>'.$access_token->errcode;
    echo '<br/><h2>错误信息:</h2>'.$access_token->errmsg;
    exit;
}
$user_info_url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token->access_token.'&openid='.$access_token->openid.'&lang=zh_CN';
//转成对象
$user_info = json_decode(file_get_contents($user_info_url));
if (isset($user_info->errcode)) {
    echo '<h1>错误:</h1>'.$user_info->errcode;
    echo '<br/><h2>错误信息:</h2>'.$user_info->errmsg;
    exit;
}
//打印用户信息
echo '<pre>';
print_r($user_info);
echo '</pre>';
?>

这里写图片描述

5、在你的微信打开连接www.你的域名.com/getcodeurl.php 成功拿到用户信息

方法二(不介绍,只上我自己看得懂的代码):

/**
 * 检查是否是微信浏览器访问
 */
$user_agent = $_SERVER['HTTP_USER_AGENT'];
if (strpos($user_agent, 'MicroMessenger') === false) {
    //不是微信端访问
} else {
    //没登陆
    if(!$_SESSION['user_id']){
        $code = isset($_GET['code']) ? $_GET['code'] : null;

        if($code == null)
        {                
            $redirect_uri = "http://".$_SERVER['HTTP_HOST']."/mobile/wx_login.php";
            // snsapi_userinfo 授权获取用户资料     snsapi_base 不用授权,只能获取用户openid
            $str='https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx170***aa4ed49&redirect_uri='.$redirect_uri.'&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect';
            header ("Location:".$str);//跳转后,$back_act将等于空
            exit;
        }

        $code = $_GET['code'];//获取code
        $weixin =  file_get_contents("https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx170***7aa4ed49&secret=1203896***657c0085c359&code=".$code."&grant_type=authorization_code");//通过code换取网页授权access_token
        $jsondecode = json_decode($weixin); //对JSON格式的字符串进行编码
        $array = get_object_vars($jsondecode);//转换成数组
        $openid = $array['openid'];//输出openid
        $access_token = $array['access_token'];
        //print_r($array);die;

        //微信用户数据
        $info = file_get_contents("https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openid&lang=zh_CN");
        $info_jsondecode = json_decode($info); //对JSON格式的字符串进行编码
        $info_array = get_object_vars($info_jsondecode);//转换成数组

        //处理一下微信用户名
        $nickname = $info_array['nickname'];
        $nickname = preg_replace("/[^\x{4e00}-\x{9fa5}0-9a-zA-Z]/iu",'',$nickname);

        //以下是保存用户信息的操作

    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
微信H5网页授权是指在使用微信浏览器访问H5网页时通过微信授权登录,获取用户基本信息。这个过程分为三个步骤:引导用户授权获取授权码、通过授权获取用户信息。 首先,用户进入H5网页后,网页需要引导用户进行授权登录。网页可以通过调用微信JS-SDK中的微信授权接口,弹出微信授权登录的窗口。用户点击确认后,微信会生成一个授权码,并跳转回H5网页。 然后,网页需要使用授权码去微信服务器获取用户基本信息。网页可以通过HTTP请求,将授权码发送给微信服务器的接口,并附上AppID和AppSecret等参数。微信服务器验证授权码的有效性后,会返回用户基本信息,如openid、昵称、头像等。 最后,网页可以根据获取用户基本信息,进行相应的业务操作。比如显示用户的头像和昵称,或者根据openid等唯一标识,将用户与其它业务系统进行关联。 需要注意的是,进行微信H5网页授权需要先申请微信开放平台的开发者账号,并创建一个公众号或移动应用。通过在微信开放平台进行配置,获取AppID和AppSecret等必要的参数,用于网页授权的流程中。 总结起来,微信H5网页授权获取用户基本信息是通过使用微信授权接口,引导用户进行授权登录,再通过授权码和微信服务器进行交互,最终获取用户基本信息。这个过程可以实现在H5网页上使用微信账号登录,并获取用户信息的功能。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

每天都进步一点点

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值