微信公衆號開發獲取用戶信息

1、獲取用戶基本信息接口

在關注者與公衆號產生消息交互後,公衆號可得到關注者的OpenID(加密後的微信號,每一個用戶對每一個公衆號的OpenID是惟一的。對於不一樣公衆號,同一用戶的openid不一樣)。公衆號可經過本接口來根據OpenID獲取用戶基本信息,包括暱稱、頭像、性別、所在城市、語言和關注時間。php

獲取用戶基本信息

開發者可經過OpenID來獲取用戶基本信息。請使用https協議。css

接口調用請求說明html

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

參數說明jquery

參數 是否必須 說明
access_token 調用接口憑證
openid 普通用戶的標識,對當前公衆號惟一
lang 返回國家地區語言版本,zh_CN 簡體,zh_TW 繁體,en 英語

返回說明web

正常狀況下,微信會返回下述JSON數據包給公衆號: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
}
複製代碼

 

參數說明api

參數 說明
subscribe 用戶是否訂閱該公衆號標識,值爲0時,表明此用戶沒有關注該公衆號,拉取不到其他信息。
openid 用戶的標識,對當前公衆號惟一
nickname 用戶的暱稱
sex 用戶的性別,值爲1時是男性,值爲2時是女性,值爲0時是未知
city 用戶所在城市
country 用戶所在國家
province 用戶所在省份
language 用戶的語言,簡體中文爲zh_CN
headimgurl 用戶頭像,最後一個數值表明正方形頭像大小(有0、4六、6四、9六、132數值可選,0表明640*640正方形頭像),用戶沒有頭像時該項爲空
subscribe_time 用戶關注時間,爲時間戳。若是用戶曾屢次關注,則取最後關注時間

錯誤時微信會返回錯誤碼等信息,JSON數據包示例以下(該示例爲AppID無效錯誤):微信

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

2、程序實現

程序一: 獲取用戶的受權,而後再跳轉頁面的顯示用戶的信息app

注意:紅色字體部分不要忘記替換成本身的微信公衆平臺

<?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, true);
//p($userInfo_arr);

$headimgurl =  $userInfo_arr['headimgurl']; 
$headimgurl_tmpl =  substr($headimgurl, 0, -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', 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 for="textinput1">
                用戶的惟一標識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"
            type="text">
        </div>
        <div data-role="fieldcontain">
            <label for="textinput3">
                性別(1時是男性,值爲2時是女性,值爲0時是未知):
            </label>
            <input name="" id="textinput3" placeholder="" value="<?php echo $userInfo_arr['sex']; ?>" data-mini="true"
            type="text">
        </div>
        <div data-role="fieldcontain">
            <label for="textinput4">
                客戶端所用的語言:
            </label>
            <input name="" id="textinput4" placeholder="" value="<?php echo $userInfo_arr['language']; ?>" data-mini="true"
            type="text">
        </div>
        <div data-role="fieldcontain">
            <label for="textinput5">
                城市:
            </label>
            <input name="" id="textinput5" placeholder="" value="<?php echo $userInfo_arr['city']; ?>" data-mini="true"
            type="text">
        </div>
        <div data-role="fieldcontain">
            <label for="textinput7">
                省份:
            </label>
            <input name="" id="textinput7" placeholder="" value="<?php echo $userInfo_arr['province']; ?>" data-mini="true"
            type="text">
        </div>
        <div data-role="fieldcontain">
            <label for="textinput8">
                國家:
            </label>
            <input name="" id="textinput8" placeholder="" value="<?php echo $userInfo_arr['country']; ?>" data-mini="true"
            type="text">
        </div>
        <div data-role="fieldcontain">
            <label 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>
相關文章
相關標籤/搜索