Home >WeChat Applet >WeChat Development >Development example of personalized menu in WeChat public account
Personalized menu allows different user groups of the public account to see different customized menus. This interface is open to certified subscription accounts and certified service accounts. The personalized menu requires the user's WeChat client version to be iPhone 6.2.2, Android 6.2.4 or above. Other versions of WeChat are not supported for the time being. In addition, you must first define a personalized menu. Define the default menu.
Developers can set the menu that users see through the following conditions:
User tags (Developers can use user tags to meet their business needs to complete)
gender
Mobile operating system
Region (the region set by the user on the WeChat client)
Language (the language set by the user on the WeChat client)
Description of personalized menu matching rules:
Updates to the personalized menu will be overwritten.
For example, the public account has released the default menu, personalized menu 1, personalized menu 2, and personalized menu 3. Then when the user enters the official account page, matching will start from personalized menu 3. If personalized menu 3 is successfully matched, it will directly return to personalized menu 3. Otherwise, it will continue to try to match personalized menu 2 until a menu is successfully matched.
According to the above matching rules, in order to avoid confusion about the menu's effective time, it has been decided not to provide a personalized menu editing API. When developers need to update the menu, they need to re-release the complete configuration.
The project structure of this example is as follows. In addition to the files in the previous chapter, three files for processing personalized menus have been added.
addconditional_menu.php: Create a personalized menu
trymatch.php: Test the matching personalized menu
delconditional_menu.php: Delete personalized menu
The interface for creating a personalized menu is https://api.weixin.qq.com /cgi-bin/menu/addconditional?access_token=ACCESS_TOKEN, send the menu string in post mode.
addconditional_menu.php
<?php require_once('./Utils.php'); //个性化菜单字符串 $menujson = '{ "button":[ { "type":"click", "name":"单击", "key":"V1001_TODAY_MUSIC" }, { "name":"搜素", "sub_button":[ { "type":"view", "name":"百度", "url":"http://www.baidu.com/" } ] }], "matchrule":{ "sex":"1" } }'; $url = "https://api.weixin.qq.com/cgi-bin/menu/addconditional?access_token=".Utils::get_access_token(); //创建个性化菜单 $result = Utils::https_request($url, $menujson); //返回返回menuid表示成功 echo $result;
The return result is as follows:
View the menu and find that it has changed from the default menu to a personalized menu , as shown below
#Only gender filtering is set here, 1 means male. For other condition settings, please refer to the personalized menu interface.
The test interface is https://api.weixin.qq.com/cgi-bin/menu/trymatch?access_token=ACCESS_TOKEN, which requires post " user_id" past.
trymatch.php
<?php @header('Content-type: text/plain;charset=UTF-8'); require_once('./Utils.php'); $url = "https://api.weixin.qq.com/cgi-bin/menu/trymatch?access_token=".Utils::get_access_token(); //user_id可以是粉丝的OpenID,也可以是粉丝的微信号 $userID = '{"user_id":"o4WmZ0h-4huBUVQUczx2ezaxIL9c"}'; $result = Utils::https_request($url, $userID); echo $result; ?>
The returned results are as follows:
The interface to delete the personalized menu is https://api.weixin.qq.com/cgi-bin/menu/delconditional?access_token=ACCESS_TOKEN, and you need to post "menuid".
delconditional_menu.php
<?php @header('Content-type: text/plain;charset=UTF-8'); require_once('./Utils.php'); $url = "https://api.weixin.qq.com/cgi-bin/menu/delconditional?access_token=".Utils::get_access_token(); //menuid,个性化菜单的menuid $menuID = '{"menuid":414784577}'; $result = Utils::https_request($url, $menuID); echo $result; ?>
The return result is as follows to indicate success:
Use The common custom menu query interface can obtain the default menu and all personalized menu information, please refer to the previous chapter.
The results after the query are as follows:
The menuid here is different from the above because I deleted the previous one Personalize the menu and then reinitialize it.
Use the common custom menu deletion interface to delete all custom menus (including default menus and all personalized menus)
The above is the detailed content of Development example of personalized menu in WeChat public account. For more information, please follow other related articles on the PHP Chinese website!