首页 > 微信 > 详细

微信网页授权获取用户openid及用户信息

时间:2019-02-19 14:22:20      阅读:189      评论:0      收藏:0      [点我收藏+]
$code = $_GET["code"];//获取code
$appid=“xxxx”;//公众号appid
$APPSECRET="xxx";//公众号appsecret
$url = ‘http://‘.$_SERVER[‘HTTP_HOST‘].$_SERVER[‘REQUEST_URI‘];//获取当前访问的网址url含参数标识,重定向用
$redirect_uri = urlencode ($url);//url转码
if($code){
    $oauth2Url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$APPSECRET&code=$code&grant_type=authorization_code";
    $oauth2 = request_get($oauth2Url);
    $access_token = $oauth2["access_token"];
    $openId = $oauth2[‘openid‘];
    $get_user_info_url = "https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openId&lang=zh_CN";
    $userInfo = request_get($get_user_info_url);//用户信息,一个对象数组
    $openId=$userInfo[‘openid‘];//用户openid
    $nickName=$userInfo[‘nickname‘];//用户微信信息中的昵称
    $sex=$userInfo[‘sex‘];//用户微信信息中的性别
    $wxCity=$userInfo[‘city‘];//用户微信信息中的所在城市    
}else{
   $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);
}

微信网页授权获取用户openid及用户信息

原文:https://www.cnblogs.com/zmq777/p/10400645.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!