获取微信公众号的粉丝openid以及用openid获取unionID

时间:2024-03-05 18:14:42

第一步获取微信粉丝的openid

https://api.weixin.qq.com/cgi-bin/user/get?access_token=access_token

access_token这里就不说了,用appid和appSecret就能获取到

这里微信官方文档说的是 一次拉取调用最多拉取10000个关注者的OpenID,可以通过多次拉取的方式来满足需求

接口地址:https://developers.weixin.qq.com/doc/offiaccount/User_Management/Getting_a_User_List.html

第二步通过openid获取到用户信息及unionID

https://api.weixin.qq.com/cgi-bin/user/info/batchget?access_token=access_token

post提交数据

{
   "user_list": [
       {
           "openid": "osJk_w6Lvj8KdUVAZt_QuUGvRMzU", 
           "lang": "zh-CN"
       }, 
       {
           "openid": "osJk_w748mQUNB8jbpKn_9auRkRM", 
           "lang": "zh-CN"
       }
   ]
}

正常情况下会返回

{
    "subscribe": 1, 
    "openid": "o6_bmjrPTlm6_2sgVt7hMZOPfL2M", 
    "nickname": "Band", 
    "sex": 1, 
    "language": "zh_CN", 
    "city": "广州", 
    "province": "广东", 
    "country": "中国", 
    "headimgurl":"http://thirdwx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ4eMsv84eavHiaiceqxibJxCfHe/0",
    "subscribe_time": 1382694957,
    "unionid": " o6_bmasdasdsad6_2sgVt7hMZOPfL"
    "remark": "",
    "groupid": 0,
    "tagid_list":[128,2],
    "subscribe_scene": "ADD_SCENE_QR_CODE",
    "qr_scene": 98765,
    "qr_scene_str": ""
}

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

 

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