Home  >  Article  >  Backend Development  >  PHP implements labeling skills in WeChat mini programs

PHP implements labeling skills in WeChat mini programs

WBOY
WBOYOriginal
2023-06-01 09:01:521302browse

With the popularity of WeChat mini programs, more and more companies are beginning to use them as a promotional and marketing channel. In the daily operation of mini programs, it is often necessary to label different users for better and accurate push and management. How to implement the labeling function in the mini program is a skill that operators need to master.

This article will share some labeling techniques in WeChat mini programs using PHP, hoping to help those mini program operators who need to label.

  1. Get the Mini Program Access Token

When using the WeChat API, you need to obtain the Access Token first in order to obtain the interface data. The API interface for obtaining Access Token in the mini program is as follows:

https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET

Among them, APPID and APPSECRET need to be replaced with the AppId and AppSecret of your own mini program.

You can use the following code to obtain the Access Token in PHP:

$appid = 'your_appid';//小程序的appid
$secret = 'your_secret';//小程序的secret
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$secret}";
$res = file_get_contents($url);
$res = json_decode($res, true);
$access_token = $res['access_token'];
  1. Get the user OpenID

Before labeling the user, you need to obtain the user first OpenID in order to call WeChat API for labeling operations. The API interface for obtaining the user's OpenID in the mini program is as follows:

https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code

Among them, JSCODE is the code returned by the mini program calling wx.login(). In PHP, you can use the following code to obtain the user's OpenID:

$appid = 'your_appid';//小程序的appid
$secret = 'your_secret';//小程序的secret
$js_code = $_GET['code'];//小程序登录时获取的code
$url = "https://api.weixin.qq.com/sns/jscode2session?appid={$appid}&secret={$secret}&js_code={$js_code}&grant_type=authorization_code";
$res = file_get_contents($url);
$res = json_decode($res, true);
$openid = $res['openid'];
  1. Tag the user

After obtaining the user's OpenID, you can call the WeChat API to tag the user. The API interface for tagging users in the mini program is as follows:

https://api.weixin.qq.com/cgi-bin/tags/members/batchtagging?access_token=ACCESS_TOKEN

Among them, ACCESS_TOKEN is the Access Token obtained in step 1. You can use the following code to tag users in PHP:

$tags = array(101, 102);//需要打标签的标签 ID
$data = array(
    'openid_list' => array($openid),//用户的openid列表
    'tagid_list' => $tags,//标签 ID 列表
);
$json = json_encode($data);
$url = "https://api.weixin.qq.com/cgi-bin/tags/members/batchtagging?access_token={$access_token}";
$res = http_request($url, $json);//调用自定义方法 http_request()
$res = json_decode($res, true);
if ($res['errcode'] == 0) {//打标签成功
    echo '打标签成功!';
} else {//打标签失败
    echo '打标签失败!';
}

//自定义方法 http_request()
function http_request($url, $data = null)
{
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    if (!empty($data)) {
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    }
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($curl);
    curl_close($curl);
    return $output;
}

The above is the tagging technique in WeChat applet using PHP. It should be noted that the Access Token needs to be valid when calling the WeChat API. , otherwise an "Access Denied" error will occur. If the Access Token expires, you can re-call the interface to obtain the Access Token to update it.

The above is the detailed content of PHP implements labeling skills in WeChat mini programs. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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