Public platform development 5-examples of common interface calls and resolutions (including source code) in user management, 5-including source code
Public platform development-examples of common interface calls and resolutions (including source code) in user management)
Author: Meng xianglei-public platform development example tutorial
The first step to grasp the user is to save the followers information that has been followed, and the interface for getting the user list is used. The public account can be obtained through this interface. The list of accounts is composed of a string of openids (encrypted numbers, each user has a unique OpenID for each public account. One pull call can pull the OpenID of up to 10000 referers. You can use multiple pull requests to meet your requirements.
1. Obtain the list of users that follow the user list interface to call the instance
Interface Description
Http Request Method: GET
Interface call address:
Https://api.weixin.qq.com/cgi-bin/user/get? Access_token = ACCESS_TOKEN & next_openid = NEXT_OPENID
The request parameters are described in the following table:
Parameters |
Required? |
Description |
Access_token |
Yes |
Interface call credential |
Next_openid |
Yes |
The first OPENID to be pulled. If this parameter is not specified, the OPENID is pulled from the beginning by default. |
Return description:
Under normal circumstances, a JSON data packet is returned to the public number, as shown below:
{"Total": 2, "count": 2, "data": {"openid": ["", "OPENID1", "OPENID2"]}, "next_openid ": "NEXT_OPENID "}
The following table describes the response parameters:
Parameters |
Description |
Total |
Follow the total number of users of this public account |
Count |
The number of openids pulled. The maximum value is 10000. |
Data |
List data, OPENID list |
Next_openid |
Pull the OPENID of the last user in the list |
Use the program to call the interface to obtain the Code:
<? Php/** get follow user list 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']; // get follow user list interface $ userurl = "https://api.weixin.qq.com/cgi-bin/user/get? Access_token = ". $ token. ""; // call $ userarr = (array) json_decode (getdata ($ userurl) through the getdata interface; // process the returned information and output $ useropenidarr = (array) $ userarr ['data']; print_r ($ useropenidarr);?>
Code Parsing
Require ('wei _ function. php'); contains wei_function.php. You can purchase the function file "public platform development instance tutorial", which is detailed on page 95th.
Similar to public platform development 3-Server IP interface instance (including source code), after obtaining access_token, replace
$ Userurl = "https://api.weixin.qq.com/cgi-bin/user/get? Access_token = ". $ token ."";
The access_token parameter in, and get the returned information through the getdata () function. After processing, print it ,.
If you want to focus on more than 10000 users, you only need to add the & next_openid = NEXT_OPENID parameter after the interface. NEXT_OPENID will return this value in the previous call, for example:
$ Userurl = "https://api.weixin.qq.com/cgi-bin/user/get? Access_token = ". $ token." & next_openid = NEXT_OPENID ";
Ii. Call the instance through the basic user information interface (UnionID mechanism)
After obtaining the user's OpenID through the follow user list interface, you can obtain the user's basic information through this parameter and call the get user basic information (UnionID mechanism) interface, for example: nickname, city, gender, user profile picture, whether to pay attention to public accounts and other information. To better understand users, you need to save the information to the database together.
Interface Description
Http Request Method: GET
Interface call address:
Https://api.weixin.qq.com/cgi-bin/user/info? Access_token = ACCESS_TOKEN & openid = OPENID & lang = zh_CN
The request parameters are described in the following table:
Parameters |
Required? |
Description |
Access_token |
Yes |
Interface call credential |
Openid |
Yes |
The identifier of a common user, which is unique to the current public account. |
Lang |
No |
Return to the Chinese language version, zh_CN simplified, zh_TW traditional, en english |
Return description:
Under normal circumstances, a JSON data packet is returned to the public number, as shown below:
{
"Subscribe": 1,
"Openid": "o6_bmjrPTlm6_2sgVt7hMZOPfL2M ",
"Nickname": "Band ",
"Sex": 1,
"Language": "zh_CN ",
"City": "Guangzhou ",
"Province": "Guangdong ",
"Country": "China ",
"Headimgurl": "http://wx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ4
EMsv84eavHiaiceqxibJxCfHe/0 ",
"Subscribe_time": 1382694957,
"Unionid": "o6_bmasdasdsad6_2sgVt7hMZOPfL"
"Remark ":"",
"Groupid": 0,
"Tagid_list": []
}
The following table describes the response parameters:
Parameters |
Description |
Subscribe |
Whether the user subscribes to the public account ID. If the value is 0, the user does not pay attention to the public account and cannot obtain other information. |
Openid |
User ID, which is unique to the current public account |
Nickname |
User nickname |
Sex |
Gender of the user. If the value is 1, the user is male. If the value is 2, the user is female. If the value is 0, the user is unknown. |
City |
User's City |
Country |
Country of the user |
Province |
Province |
Language |
The user's language. The simplified Chinese name is zh_CN. |
Headimgurl |
User profile picture. The last value indicates the size of the square profile picture (optional values include 0, 46, 64, 96, and 132, and 0 indicates the size of the 640x640 square profile picture ), this item is blank when the user does not have an avatar. If you change the profile picture, the original profile picture URL becomes invalid. |
Subscribe_time |
The user's attention time, which is the timestamp. If the user has followed the last time |
Unionid |
This field appears only when you bind a public account to an open platform account. |
Remark |
Comments from public account operators to fans. Public Account operators can add comments to fans in the user management field of the public platform. |
Groupid |
ID of the user's group (compatible with the old user group Interface) |
Tagid_list |
User ID list |
Use the program to call the interface to obtain the Code:
<? Php/** get basic user information */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']; // get follow user list interface $ userurl = "https://api.weixin.qq.com/cgi-bin/user/get? Access_token = ". $ token. ""; // call $ userarr = (array) json_decode (getdata ($ userurl) through the getdata interface; // process the returned information and output $ useropenidarr = (array) $ userarr ['data']; foreach ($ useropenidarr ['openid'] as $ value) {// get user basic information cyclically $ 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/>" ;}?>
Code Parsing
Require ('wei _ function. php'); contains wei_function.php. You can purchase the function file "public platform development instance tutorial", which is detailed on page 95th.
After obtaining the user's OpenID list, obtain the user's basic information based on each OpenID. The foreach loop is used here.
Foreach ($ useropenidarr ['openid'] as $ value ){
// Obtain basic user information cyclically
$ 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/> ";
}
Running result.
To save user information to the database, simply replace print_r ($ infoarr) with the code for adding the database, as shown in:
Foreach ($ useropenidarr ['openid'] as $ value) {// get basic user information cyclically $ infourl = "https://api.weixin.qq.com/cgi-bin/user/info? Access_token = ". $ token. "& openid = ". $ value. "& lang = zh_CN"; $ infoarr = (array) json_decode (getdata ($ infourl )); // Add User information to the database $ SQL = "insert into userinfo ('nickname', 'sex', 'city') values ('". $ infoarr ['nickname']. "','". $ infoarr ['sex']. "','". $ infoarr ['city']. "')"; mysql_query ($ SQL );}