网页授权获取用户基本信息

微信授权登录。

基本情况:认证的服务号;

主要测试代码:

 public function openOAuth(){
        //获取回调连接中带有的code
        $code = $_GET['code'];
        //获取access_token openid
        $json = file_get_contents("https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx7a5423a6f023716c5&secret=6bb7bstddekxrsa3bdcd296979bfb66967f41&code=$code&grant_type=authorization_code");
        $json = json_decode($json);

        $refresh_token =  $json->refresh_token;
      //通过refresh_token 获取需要的access_token $json_ref
= file_get_contents("https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=wx3a542a6f094310c5&grant_type=refresh_token&refresh_token=$refresh_token"); $json_ref = json_decode($json_ref); $access_token = $json_ref->access_token; $openId = $json_ref->openid; $json_userinfo = file_get_contents("https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openId&lang=zh_CN"); $json_userinfo = json_decode($json_userinfo); $nikename = $json_userinfo->nickname; $sex = $json_userinfo->sex; $area = $json_userinfo->province.'-'.$json_userinfo->city.'-'.$json_userinfo->country; $headimgurl = $json_userinfo->headimgurl; //echo $headimgurl; $privilege = $json_userinfo->privilege; $unionid = $json_userinfo->unionid; echo "<h1>展示身份信息</h1>"; echo"<h2>昵称:$nikename</h2>"; echo"<h2>性别:$sex</h2>"; echo"<h2>地址:$area</h2>"; echo'<h2>头像</h2><img src="'.$headimgurl.'">'; echo"<h2>privilege:$privilege </h2>"; echo"<h2>unionid:$unionid</h2>"; }

个人完整文档;https://files.cnblogs.com/files/bin-pureLife/%E6%8E%88%E6%9D%83.zip

APPID 等隐私处以替换。

posted @ 2014-11-21 18:22  鱼尾纹  阅读(486)  评论(0编辑  收藏  举报