php微信网页授权登录获取用户基本信息

现在就说说,用微信网页授权获取用户基本信息

 

首先重要的条件

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.com/mobile/ceshi/weixin/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

<?php
header("Content-type: text/html; charset=utf-8");
$appid = "XXXXXXX";  
$secret = "xxxxxxxxxxxxxxxxxxxxxx";
$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);
}

?>

 

如果没关注的话,获取不到基本信息,就获取openid

 

 

weixin2文件夹建两个文件  index.php    getUserInfo.php

<?php

$appid='XXXXXXXXXXXXXXXXXXXXX';
$redirect_uri = urlencode ( 'http://www.xxxxxx.com/mobile/ceshi/weixin2/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);

?>

 

<?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.com/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.com/cgi-bin/user/info?access_token=$access_token&openid=$openid&lang=zh_CN";
$get_user_info_url = "https://api.weixin.qq.com/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都相同

 

 

 

 

 

 

 

 

 

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

明雨星云

感谢,我会继续创作更优质作品

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

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

打赏作者

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

抵扣说明:

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

余额充值