编程爱好者联盟 2017-03-09
微信公众平台开发-用户管理中常用接口调用实例及解析(含源码)作者: 孟祥磊-《微信公众平台开发实例教程》
掌握用户的第一步就是将已经关注的粉丝信息保存起来,这个时候就用到获取用户列表接口。公众号可通过本接口来获取帐号的关注者列表,关注者列表由一串OpenID(加密后的微信号,每个用户对每个公众号的OpenID是唯一的)组成。一次拉取调用最多拉取10000个关注者的OpenID,可以通过多次拉取的方式来满足需求。
一、 获取微信关注用户列表接口调用实例
接口说明
http请求方式:GET
接口调用地址:
https://api.weixin.qq.com/cgi-bin/user/get?access_token=ACCESS_TOKEN&next_openid=NEXT_OPENID
请求参数说明,如表所示:
| 参数 | 是否必须 | 说明 |
返回说明:
正常情况下,微信会返回JSON数据包给公众号,如下所示:
{"total":2,"count":2,"data":{"openid":["","OPENID1","OPENID2"]},"next_openid":"NEXT_OPENID"}
返回信息参数说明,如表所示:
| 参数 | 说明 |
使用程序调用接口获取,代码:
<?php
/*
*获取微信关注用户列表OpenID
*/
require('wei_function.php');
$appid="wx78478e595939c538";
$secret="5540e8ccab4f71dfad752f73cfb85780";
$url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret."";
$output=getdata($url);
$tokenarr=(array)json_decode($output);
$token=$tokenarr['access_token'];
//获取关注用户列表接口
$userurl="https://api.weixin.qq.com/cgi-bin/user/get?access_token=".$token."";
//通过getdata进行接口调用
$userarr=(array)json_decode(getdata($userurl));
//将返回信息进行处理并输出
$useropenidarr=(array)$userarr['data'];
print_r($useropenidarr);
?> 代码解析
require('wei_function.php');包含wei_function.php,该函数文件可以购买《微信公众平台开发实例教程》,在该书中第95页有详细讲解。
与微信公众平台开发3-微信服务器IP接口实例(含源码)的获取微信服务器IP一样,获取到access_token后,替换
$userurl="https://api.weixin.qq.com/cgi-bin/user/get?access_token=".$token."";
中的access_token参数,并通过getdata()函数获取返回的信息,处理后,进行打印,如图所示。

如果关注用户大于10000,需多次调用,只需在接口后增加&next_openid=NEXT_OPENID的参数,NEXT_OPENID会在前一次调用时返回该值,如:
$userurl="https://api.weixin.qq.com/cgi-bin/user/get?access_token=".$token."&next_openid=NEXT_OPENID";
二、用户基本信息接口(UnionID机制)调用实例
在通过获取关注用户列表接口获取到用户的OpenID后,可通过该参数并调用获取用户基本信息(UnionID机制)接口获取用户的基本信息,如:昵称、城市、性别、用户头像、是否关注公众号等信息,为了更好的了解用户,需要将这些信息一同保存到数据库中。
接口说明
http请求方式:GET
接口调用地址:
https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN
请求参数说明,如表所示:
| 参数 | 是否必须 | 说明 |
返回说明:
正常情况下,微信会返回JSON数据包给公众号,如下所示:
{
"subscribe": 1,
"openid": "o6_bmjrPTlm6_2sgVt7hMZOPfL2M",
"nickname": "Band",
"sex": 1,
"language": "zh_CN",
"city": "广州",
"province": "广东",
"country": "中国",
"headimgurl": "http://wx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ4
eMsv84eavHiaiceqxibJxCfHe/0",
"subscribe_time": 1382694957,
"unionid": " o6_bmasdasdsad6_2sgVt7hMZOPfL"
"remark": "",
"groupid": 0,
"tagid_list":[128,2]
}
返回信息参数说明,如表所示:
| 参数 | 说明 |
使用程序调用接口获取,代码:
<?php
/*
*获取微信关注用户基本信息
*/
require('wei_function.php');
$appid="wx78478e595939c538";
$secret="5540e8ccab4f71dfad752f73cfb85780";
$url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret."";
$output=getdata($url);
$tokenarr=(array)json_decode($output);
$token=$tokenarr['access_token'];
//获取关注用户列表接口
$userurl="https://api.weixin.qq.com/cgi-bin/user/get?access_token=".$token."";
//通过getdata进行接口调用
$userarr=(array)json_decode(getdata($userurl));
//将返回信息进行处理并输出
$useropenidarr=(array)$userarr['data'];
foreach ($useropenidarr['openid'] as $value) {
//循环获取用户基本信息
$infourl="https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$token."&openid=".$value."&lang=zh_CN";
$infoarr=(array)json_decode(getdata($infourl));
print_r($infoarr);
echo "<br />";
}
?> 代码解析
require('wei_function.php');包含wei_function.php,该函数文件可以购买《微信公众平台开发实例教程》,在该书中第95页有详细讲解。
获取到用户OpenID列表后,根据每条OpenID获取用户基本信息,这里用到foreach循环
foreach ($useropenidarr['openid'] as $value) {
//循环获取用户基本信息
$infourl="https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$token."&openid=".$value."&lang=zh_CN";
$infoarr=(array)json_decode(getdata($infourl));
print_r($infoarr);
echo "<br />";
}
运行结果如图所示。

如果需要将用户信息保存到数据库,只需替换print_r($infoarr);为增加数据库的代码即可,如:
foreach ($useropenidarr['openid'] as $value) {
//循环获取用户基本信息
$infourl="https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$token."&openid=".$value."&lang=zh_CN";
$infoarr=(array)json_decode(getdata($infourl));
//将用户信息增加到数据库
$sql=”insert into userinfo(`nickname`,`sex`,`city`) values
(‘”.$infoarr[‘nickname’].”’,’”.$infoarr['sex'].”’,’”.$infoarr['city'].”’)”;
mysql_query($sql);
}