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

分类:

技术点:

相关文章: