Home > Backend Development > PHP Tutorial > How to use PHP to implement WeChat public account data statistics

How to use PHP to implement WeChat public account data statistics

WBOY
Release: 2023-05-13 19:32:01
Original
1328 people have browsed it

As WeChat public accounts gradually become one of the important tools for corporate marketing, statistical analysis of public account data has become more and more important. Through data analysis, you can understand the number of fans, activity, user conversion rate and other indicators of the official account, thereby optimizing operational strategies and improving marketing effects. This article will introduce how to use PHP to implement WeChat public account data statistics.

1. Obtain the WeChat public account interface permission

First, you need to register on the WeChat official website to become a developer of the WeChat public account. After successful registration, obtain your AppID and AppSecret in the Developer Center, and configure the interface permissions of the WeChat official account, including permissions to obtain fan lists, obtain user information, and obtain basic information of the official account.

2. Use PHP to write the interface request function

In PHP, you can use the curl function library to send a request to the WeChat official account backend to obtain the required data. Before writing the request function, you need to obtain access_token, which is one of the necessary parameters to access the WeChat official account interface. There are two ways to obtain access_token:

1. Use the interface address of the WeChat public platform and obtain the access_token by sending an http request.

2. Save the access_token on the local server, set an expiration time, and determine whether the access_token has expired before each request. If it expires, obtain it again.

The following is an example of a request function to obtain access_token:

function getAccessToken($appid,$appsecret){
    $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
    curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
    $data = curl_exec($ch);
    $http_code = curl_getinfo($ch,CURLINFO_HTTP_CODE);
    curl_close($ch);
    if($http_code == 200){
        $access_token = json_decode($data)->access_token;
        return $access_token;
    }else{
        return false;
    }
}
Copy after login

3. Write a function to obtain the fan list and user information

The interface address for obtaining the fan list and user information is as follows :

  • Get the follower list interface address:
https://api.weixin.qq.com/cgi-bin/user/get?access_token=ACCESS_TOKEN&next_openid=NEXT_OPENID
Copy after login
  • Get the user information interface address:
https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN
Copy after login

Among them, ACCESS_TOKEN is Call the interface credentials. NEXT_OPENID is the first OPENID pulled. If not filled in, it will be pulled from the beginning by default. OPENID is the identification of an ordinary user. You need to use the authorized access_token to obtain the user's information.

The following is an example function to obtain the fan list and user information:

/*
* 获取用户列表
*/
function getUserList($access_token,$next_openid=""){
    $url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=".$access_token;
    if(!empty($next_openid)){
        $url .= "&next_openid=".$next_openid;
    }
    $result = https_request($url);
    $jsoninfo = json_decode($result, true);
    return $jsoninfo;
}

/*
* 获取用户信息
*/
function getUserInfo($access_token,$openid){
    $url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$access_token."&openid=".$openid."&lang=zh_CN";
    $result = https_request($url);
    $jsoninfo = json_decode($result, true);
    return $jsoninfo;
}
Copy after login

4. Write a function to obtain the basic information of the public account

The interface address to obtain the basic information of the public account As follows:

https://api.weixin.qq.com/cgi-bin/account/getaccountinfo?access_token=ACCESS_TOKEN
Copy after login

Among them, ACCESS_TOKEN is the calling interface credential.

The following is an example function to obtain the basic information of the public account:

/*
* 获取公众号信息
*/
function getAccountInfo($access_token){
    $url = "https://api.weixin.qq.com/cgi-bin/account/getaccountinfo?access_token=".$access_token;
    $result = https_request($url);
    $jsoninfo = json_decode($result, true);
    return $jsoninfo;
}
Copy after login

5. Data statistical analysis

After obtaining the fan list, user information and the basic information of the public account, Various statistical analyzes of data can be performed. For example, you can count the gender distribution, regional distribution, attention channels, user conversion rate and other data of public account followers.

The following is a sample code that counts the gender distribution of followers of a public account:

/*
* 获取用户性别比例
*/
function getUserSexRatio($access_token){
    $user_list = getUserList($access_token);
    $count = $user_list["total"];
    $user_openid_list = $user_list["data"]["openid"];
    $male_count = 0;
    $female_count = 0;
    foreach($user_openid_list as $openid){
        $user_info = getUserInfo($access_token,$openid);
        if($user_info["sex"] == 1){
            $male_count++;
        }elseif($user_info["sex"] == 2){
            $female_count++;
        }
    }
    $male_ratio = sprintf("%.2f",$male_count/$count*100)."%";
    $female_ratio = sprintf("%.2f",$female_count/$count*100)."%";
    $result = array("male_ratio"=>$male_ratio,"female_ratio"=>$female_ratio);
    return $result;
}
Copy after login

Through the above function, the gender ratio data of followers of a public account can be obtained.

Realizing WeChat public account data statistics through PHP, you can understand the operation status of the public account in a timely manner, so as to optimize and improve it, and further improve the operating effect of the public account.

The above is the detailed content of How to use PHP to implement WeChat public account data statistics. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template