Home >Backend Development >PHP Tutorial >PHP implements WeChat applet adding friends and following skills
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
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.
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:
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!