微信公众号开发获取用户信息

一、获取用户基本信息接口

在关注者与公众号产生消息交互后,公众号可获得关注者的OpenID(加密后的微信号,每个用户对每个公众号的OpenID是唯一的。对于不同公众号,同一用户的openid不同)。公众号可通过本接口来根据OpenID获取用户基本信息,包括昵称、头像、性别、所在城市、语言和关注时间。

获取用户基本信息

开发者可通过OpenID来获取用户基本信息。请使用https协议。

接口调用请求说明

http请求方式: GET
https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN

参数说明

参数 是否必须 说明
access_token 调用接口凭证
openid 普通用户的标识,对当前公众号唯一
lang 返回国家地区语言版本,zh_CN 简体,zh_TW 繁体,en 英语

返回说明

正常情况下,微信会返回下述JSON数据包给公众号:

复制代码

{
    "subscribe": 1,"openid": "o7Lp5t6n59DeX3U0C7Kric9qEx-Q","nickname": "方倍","sex": 1,"language": "zh_CN","city": "深圳","province": "广东","country": "中国","headimgurl": "http://wx.qlogo.cn/mmopen/Kkv3HV30gbEZmoo1rTrP4UjRRqzsibUjT9JClPJy3gzo0NkEqzQ9yTSJzErnsRqoLIct5NdLJgcDMicTEBiaibzLn34JLwficVvl6/0","subscribe_time": 1389684286
}

复制代码

 

参数说明

参数 说明
subscribe 用户是否订阅该公众号标识,值为0时,代表此用户没有关注该公众号,拉取不到其余信息。
openid 用户的标识,对当前公众号唯一
nickname 用户的昵称
sex 用户的性别,值为1时是男性,值为2时是女性,值为0时是未知
city 用户所在城市
country 用户所在国家
province 用户所在省份
language 用户的语言,简体中文为zh_CN
headimgurl 用户头像,最后一个数值代表正方形头像大小(有0、46、64、96、132数值可选,0代表640*640正方形头像),用户没有头像时该项为空
subscribe_time 用户关注时间,为时间戳。如果用户曾多次关注,则取最后关注时间

错误时微信会返回错误码等信息,JSON数据包示例如下(该示例为AppID无效错误):

{"errcode":40013,"errmsg":"invalid appid"}

二、程序实现

程序一: 获取用户的授权,然后再跳转页面的显示用户的信息

注意:红色字体部分不要忘记替换成自己的

<?php
//scope=snsapi_base 实例
$appid='wx87b99a9999266';
$redirect_uri = urlencode ( 'http://http://127.0.0.1/YiBaoJH/login.html' );
$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);

?>

程序二: 显示用户的信息页面,

<?php
require_once "lib.php";
$appid = "wx89999999266";  
$secret = "4dd3a9999999999993fbc490f6e52";  
$code = $_GET["code"];
p($code);
 
第一步:取得openid
$oauth2Url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$secret&code=$code&grant_type=authorization_code";
$oauth2 = getCatch($oauth2Url);
$oauth2 = json_decode($oauth2,true);
p($oauth2);
  
第二步:根据全局access_token和openid查询用户信息  
$access_token = $oauth2["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";
$get_user_info_url = "https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openid&lang=zh_CN";
$userinfo_json = getCatch($get_user_info_url);
$userInfo_arr = json_decode($userinfo_json,1)">p($userInfo_arr);

$headimgurl =  $userInfo_arr['headimgurl']; 
$headimgurl_tmpl =  substr($headimgurl,-1);      
$headimgurl = $headimgurl_tmpl."132";


?>
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="initial-scale=1.0,user-scalable=no">
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">
  <title>微信公众平台应用开发实战(第二版)</title>
    
  <link rel="stylesheet" href="https://d10ajoocuyu32n.cloudfront.net/mobile/1.3.1/jquery.mobile-1.3.1.min.css">
  
  <!-- Extra Codiqa features -->
  <link rel="stylesheet" href="codiqa.ext.css">
  
  <!-- jQuery and jQuery Mobile -->
  <script src="https://d10ajoocuyu32n.cloudfront.net/jquery-1.9.1.min.js"></script>
  <script src="https://d10ajoocuyu32n.cloudfront.net/mobile/1.3.1/jquery.mobile-1.3.1.min.js"></script>

  <!-- Extra Codiqa features -->
  <script src="https://d10ajoocuyu32n.cloudfront.net/codiqa.ext.js"></script>
  <script>
        document.addEventListener('WeixinJSBridgeReady',1)">function onBridgeReady() {
                WeixinJSBridge.call('hideOptionMenu');
                WeixinJSBridge.call('hideToolbar');
        });
  </script>
</head>
<body>
<!-- Home -->
<div data-role="page" id="page1">
    <div data-role="content">
        <div style="width:132px ; height: 132px; background-color: #fbfbfb; border: 1px solid #b8b8b8;">
            <img src="<?php echo $headimgurl; ?>" alt="image">
        </div>
    <!--<div data-role="fieldcontain">
            <label for="textinput1">
                headimageurl:
            </label>
                <input name="" id="textinput1" placeholder="" value="<?php //echo $headimgurl;?>" data-mini="true"
            type="text">
        </div>-->
        <div data-role="fieldcontain">
            <label 
                用户的唯一标识openid:
            </label>
            <input name="" id="textinput1" placeholder="" value="<?php echo $userInfo_arr['openid']; ?>" data-mini="true"
            type="text">
        </div>
        <div data-role="fieldcontain">
            <label for="textinput2">
                用户昵称:
            </label>
            <input name="" id="textinput2" placeholder="" value="<?php echo $userInfo_arr['nickname']; ?>" data-mini="true"for="textinput3">
                性别(1时是男性,值为2时是女性,值为0时是未知):
            </label>
            <input name="" id="textinput3" placeholder="" value="<?php echo $userInfo_arr['sex']; ?>" data-mini="true"for="textinput4">
                客户端所用的语言:
            </label>
            <input name="" id="textinput4" placeholder="" value="<?php echo $userInfo_arr['language']; ?>" data-mini="true"for="textinput5">
                城市:
            </label>
            <input name="" id="textinput5" placeholder="" value="<?php echo $userInfo_arr['city']; ?>" data-mini="true"for="textinput7">
                省份:
            </label>
            <input name="" id="textinput7" placeholder="" value="<?php echo $userInfo_arr['province']; ?>" data-mini="true"for="textinput8">
                国家:
            </label>
            <input name="" id="textinput8" placeholder="" value="<?php echo $userInfo_arr['country']; ?>" data-mini="true"for="textinput6">
                用户特权信息:
            </label>
            <input name="" id="textinput6" placeholder="" value="<?php print_r($userInfo_arr['privilege']); ?>" data-mini="true"
            type="text">
        </div>
    </div>
    <div data-theme="a" data-role="footer" data-position="fixed">
        <h3>
            微信公众平台应用开发实战(v2)测试示例
        </h3>
    </div>
</div>
</body>
</html>

 

相关文章

网页授权获取用户信息的方法
报错config:invalid signature的解决方案
微信开发百思不得姐实战教程
详解微信开发input输入框
教你libco是如何支撑巨大数据信息量的
微信二次开发之文本消息请求与发送