Home  >  Article  >  Backend Development  >  PHP implements WeChat applet adding friends and following skills

PHP implements WeChat applet adding friends and following skills

王林
王林Original
2023-06-01 17:40:382282browse

In the development of WeChat mini programs, many developers will encounter a problem, that is, how to implement the function of adding friends and following in the mini program. This function is particularly important in some social and information mini-programs. It can help us better promote communication and interaction between users. This article will introduce how to use PHP to implement the skills of adding friends and following in WeChat applet.

1. Apply for a WeChat public platform account and bind the mini program

Before adding friends and following the WeChat mini program, we first need to register an account on the WeChat public platform and bind the mini program . For specific steps, please refer to the relevant documents of the WeChat public platform.

After binding the mini program, we need to obtain relevant information such as access_token and openid in the mini program for subsequent API calls.

2. Use interfaces to implement following and adding friends to WeChat mini programs

  1. WeChat mini program following interface

The following interface is used to implement mini programs For those who pay attention to the function, the specific interface calling code is as follows:

$url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/bizsend?access_token=".$access_token;

$data = array(
    'touser' => $openid,
    'template_id' => $template_id,
    'page' => $page,
    'miniprogram_state' => $status,
    'lang' => "zh_CN",
    'data' => $data
);

$res = json_decode(http_post($url, json_encode($data)), true);
if ($res['errcode']==0) {
    //发送成功后的逻辑处理
} else {
    //发送失败后的逻辑处理
}

Among them, $access_token is the access_token we obtained earlier, $openid is the user's openid, $template_id is the template id in the mini program, $page It refers to the page that the mini program jumps to (can be empty), $status defines the status of the mini program (can be empty), and $data is the data parameter to be passed.

  1. WeChat mini program adding friend interface

The adding friend interface can be used to implement the function of adding friends in the mini program. The specific interface calling code is as follows:

$url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$access_token;

$data = array(
    'touser' => $openid,
    'msgtype' => "text",
    'text' => array("content"=>"你好,我是{$nickname},欢迎添加我为好友!")
);

$res = json_decode(http_post($url, json_encode($data)), true);
if ($res['errcode']==0) {
    //发送成功后的逻辑处理
} else {
    //发送失败后的逻辑处理
}

Among them, $access_token is the access_token we obtained earlier, $openid is the user's openid, and $nickname is the nickname we obtained from the applet, which is used to assemble the information added by friends.

3. Avoid the WeChat mini-program adding friends and following interfaces from being blocked

We know that WeChat official has many restrictions and will ban accounts for some illegal behaviors or excessive use of APIs. In order to avoid the mini program adding friends and following interfaces being blocked, we need to pay attention to the following points:

  1. Use the API request frequency reasonably and not too frequently;
  2. Set the message content reasonably to avoid Sensitive words or illegal content appear;
  3. Comply with WeChat mini program development specifications and do not perform any illegal operations.

4. Summary

The follow and add friend functions of WeChat mini programs are particularly important for some social and information mini programs. It can help us better promote the relationship between users. communication and interaction. It is very simple and easy to understand to implement the friend-adding and following functions of the WeChat mini program through PHP. As long as we pay attention to comply with the official development specifications of WeChat, we can avoid the mini program interface being blocked.

The above is the detailed content of PHP implements WeChat applet adding friends and following skills. 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