admin管理员组文章数量:1530085
现在就说说,用微信网页授权获取用户基本信息
首先重要的条件
1公众号认证
2网页授权获取用户的基本信息(权限接口)
登录公众号平台,开发者中心,接口权限表
网页授权获取用户基本信息,填写域名
关于网页授权的两种scope的区别
1 snsapi_base为scope发起的网页授权,是用来获取进入页面的用户openid,并且是静默授权并自动跳转到回调页。
2 snsapi_userinfo为scope发起的网页授权,是用来获取用户基本信息。但这种授权需要用户手动同意,并且由于用户同意过,所以无需关注,就可在授权后获取用户基本信息
3 用户管理类接口中,获取用户基本信息,是在用户和公众号产生消息交互或关注后事件推送后,才能根据用户openid来获取用户基本信息。这个接口,包括其他微信接口,都是需要该用户关注公众号后(openid),才能调用成功的。
建立两个文件
index.php
<?php
$appid='XXXXXXXX';
$redirect_uri = urlencode ( 'http://www.XXXXX/mobile/ceshi/weixin/getUserInfo.php' );
$url ="https://open.weixin.qq/connect/oauth2/authorize?appid=$appid&redirect_uri=$redirect_uri&response_type=code&scope=snsapi_base&state=1#wechat_redirect";
header("Location:".$url);
?>
getUserInfo.php
<?php
header("Content-type: text/html; charset=utf-8");
$appid = "XXXXXXX";
$secret = "xxxxxxxxxxxxxxxxxxxxxx";
$code = $_GET["code"];
//第一步:取全局access_token
$url = "https://api.weixin.qq/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$secret";
$token = getJson($url);
//第二步:取得openid
$oauth2Url = "https://api.weixin.qq/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/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);
}
?>
如果没关注的话,获取不到基本信息,就获取openid
weixin2文件夹建两个文件 index.php getUserInfo.php
<?php
$appid='XXXXXXXXXXXXXXXXXXXXX';
$redirect_uri = urlencode ( 'http://www.xxxxxx/mobile/ceshi/weixin2/getUserInfo.php' );
$url ="https://open.weixin.qq/connect/oauth2/authorize?appid=$appid&redirect_uri=$redirect_uri&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect";
header("Location:".$url);
?>
<?php
header("Content-type: text/html; charset=utf-8");
$appid = "XXXXXXXXXXXX";
$secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$code = $_GET["code"];
//print_r($code);exit;
//第一步:取得openid
$oauth2Url = "https://api.weixin.qq/sns/oauth2/access_token?appid=$appid&secret=$secret&code=$code&grant_type=authorization_code";
$oauth2 = getJson($oauth2Url);
print_r($oauth2);exit;
//第二步:根据全局access_token和openid查询用户信息
$access_token = $oauth2["access_token"];
//print_r($access_token);exit;
$openid = $oauth2['openid'];
//$get_user_info_url = "https://api.weixin.qq/cgi-bin/user/info?access_token=$access_token&openid=$openid&lang=zh_CN";
$get_user_info_url = "https://api.weixin.qq/sns/userinfo?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);
}
?>
即使没关注也可以获取全部信息
如果要获取unionid ,就把微信公众号绑定到微信开发平台。
在同一个微信开发平台,绑定不同的小程序或app,微信公众号,那么登录获取的unionid都相同
版权声明:本文标题:php微信网页授权登录获取用户基本信息 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/xitong/1726183780a1059300.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论