Home > WeChat Applet > WeChat Development > Development example of personalized menu in WeChat public account

Development example of personalized menu in WeChat public account

一个新手
Release: 2017-09-07 09:32:13
Original
4216 people have browsed it

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:

  1. User tags (Developers can use user tags to meet their business needs to complete)

  2. gender

  3. Mobile operating system

  4. Region (the region set by the user on the WeChat client)

  5. 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

1. Create a 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(&#39;./Utils.php&#39;);
//个性化菜单字符串
$menujson = &#39;{
	"button":[
 	{
    	"type":"click",
    	"name":"单击",
     	"key":"V1001_TODAY_MUSIC"
	},
	{
		"name":"搜素",
		"sub_button":[
            {
                "type":"view",
                "name":"百度",
                "url":"http://www.baidu.com/"
            }
        ]
 }],
 "matchrule":{
  "sex":"1"
  }
}&#39;;
$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;
Copy after login

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.

2. Test the personalized menu matching results

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(&#39;Content-type: text/plain;charset=UTF-8&#39;);
    require_once(&#39;./Utils.php&#39;);
    $url = "https://api.weixin.qq.com/cgi-bin/menu/trymatch?access_token=".Utils::get_access_token();
    //user_id可以是粉丝的OpenID,也可以是粉丝的微信号
    $userID = &#39;{"user_id":"o4WmZ0h-4huBUVQUczx2ezaxIL9c"}&#39;;
    $result = Utils::https_request($url, $userID);
    echo $result;
?>
Copy after login

The returned results are as follows:


3. Delete the personalized menu

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(&#39;Content-type: text/plain;charset=UTF-8&#39;);
    require_once(&#39;./Utils.php&#39;);
    $url = "https://api.weixin.qq.com/cgi-bin/menu/delconditional?access_token=".Utils::get_access_token();
    //menuid,个性化菜单的menuid
    $menuID = &#39;{"menuid":414784577}&#39;;
    $result = Utils::https_request($url, $menuID);
    echo $result;
?>
Copy after login

The return result is as follows to indicate success:

4. Query personalized menu

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.


5. Delete all menus

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!

Related labels:
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