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

$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);
}

转载于:https://www.cnblogs.com/zmq777/p/10400645.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值