微信网页授权获取用户信息概述
许多开发者在进行微信版网页应用开发时,需要获取用户的国家、省份、城市及昵称等基本资料。本文将基于PHP语言的基础知识,深入剖析如何顺利实现这一功能。
实施该功能需满足以下前提条件:
第一,必须拥有经过认证的公众号;
第二,公众号需具备“网页授权获取用户基本信息”的权限接口。
需要注意的是,近期有反馈指出,使用公众平台申请的测试号可能无法获取到用户信息,但切换至已认证的公众账号后问题即可解决。
若您也遭遇此类情况,建议尝试在认证的公众账号环境中进行测试!感谢各位的支持。
配置授权回调页面的域名
登录微信公众平台,进入“开发者中心”,查看“接口权限表”。
定位到“网页授权获取用户基本信息”选项,点击修改并填入您的域名,具体操作如下:

确认保存设置即可生效。
关于网页授权中两种scope参数的区别说明(依据官方文档)
1、以snsapi_base为scope发起的网页授权,旨在获取进入页面用户的openid。此过程为静默授权,用户会自动跳转至回调页,感知上直接进入了业务页面。
2、以snsapi_userinfo为scope发起的网页授权,用于获取用户的基本信息。此种授权需用户手动同意,一旦授权成功,即使未关注公众号也可获取其基本信息。
3、用户管理类接口中的“获取用户基本信息接口”,需在用户与公众号产生消息交互或关注后事件推送发生后,才能根据OpenID获取用户信息。该接口及其他微信相关接口,均要求用户关注公众号后方可调用成功。
鉴于scope存在两种不同模式,下文将分别进行详细解说。
当scope设置为snsapi_base时,用户必须是公众号的关注者才能获取相关信息。
首先自行创建两个文件:index.php 和 getUserInfo.php。
代码实例展示
index.php 代码如下:
//scope=snsapi_base 实例
$appid='你的AppId';
$redirect_uri = urlencode ( 'http://你的域名/getUserInfo.php' );
$url ="https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appid&redirect_uri=$redirect_uri&response_type=code&scope=snsapi_base&state=1#wechat_redirect";
header("Location:".$url);
getUserInfo.php 代码如下:
$appid = "你的AppId";
$secret = "你的AppSecret";
$code = $_GET["code"];
//第一步:取全局access_token
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$secret";
$token = getJson($url);
//第二步:取得openid
$oauth2Url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$secret&code=$code&grant_type=authorization_code";
$oauth2 = getJson($oauth2Url);
//第三步:根据全局access_token和openid查询用户信息
$access_token = $token["access_token"];
$openid = $oauth2['openid'];
$get_user_info_url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=$access_token&openid=$openid&lang=zh_CN";
$userinfo = getJson($get_user_info_url);
//打印用户信息
print_r($userinfo);
function getJson($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
return json_decode($output, true);
}
当scope设置为snsapi_userinfo时,用户无需关注公众号即可获取信息,但界面会弹出提示让用户点击确认,类似于登录授权的过程。
代码实例展示
index.php 代码如下:
//scope=snsapi_userinfo实例
$appid='你的AppId';
$redirect_uri = urlencode ( 'http://你的域名/getUserInfo.php' );
$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);
getUserInfo.php 代码如下:
$appid = "你的AppId";
$secret = "你的AppSecret";
$code = $_GET["code"];
//第一步:取得openid
$oauth2Url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$secret&code=$code&grant_type=authorization_code";
$oauth2 = getJson($oauth2Url);
//第二步:根据全局access_token和openid查询用户信息
$access_token = $oauth2["access_token"];
$openid = $oauth2['openid'];
$get_user_info_url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=$access_token&openid=$openid&lang=zh_CN";
$userinfo = getJson($get_user_info_url);
//打印用户信息
print_r($userinfo);
function getJson($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
return json_decode($output, true);
}
测试步骤说明
在创建好 index.php 和 getUserInfo.php 两个文件之后
首先测试 scope 为 snsapi_base 的情况
1) 先关注该公众账号
2) 将网址 http://你的域名/index.php 生成一个二维码
3) 使用微信扫描二维码
接着测试 scope 为 snsapi_userinfo 的情况
1) 替换相应的代码
2) 取消对当前公众号的关注
3) 然后使用微信扫描之前生成的二维码