The service number of the public platform and the subscription number that has been successfully applied for beta testing all have the function of customizing the menu. Developers can use this function to add custom menus at the bottom of the public account's session interface. Users can click the Options in the menu to bring up corresponding reply information or webpage links. The custom menu interface provides more possibilities for the public account information display space. This article will make simple development and application for custom menus for your reference.
I. INTRODUCTION
Public PlatformService numberAndSubscription No. successfully applied for beta testing qualificationAll have custom menu functions. Developers can use this function to add custom menus at the bottom of the public account's session interface. Users can click the Options in the menu to bring up corresponding reply information or webpage links. The custom menu interface provides more possibilities for the public account information display space. This article will make simple development and application for custom menus for your reference.
2. official instructions
After obtaining the credential, the developer can use the credential to create, query, and delete user-defined menus of the public account. The custom menu interface supports the following buttons:
Click (click event ):
After you click the click type button, the server pushes the click event to the developer through the Message Interface (event type), and carries the key value entered by the developer in the button, developers can reply to a message through a custom key value.
View (webpage access ):
After clicking the view type button, the user will jump directly to the url specified by the developer.
After the custom menu is created, it takes 24 hours for the client to appear because of the client cache. We recommend that you try to remove the public account and follow it again during the test to see the effect after creation.
Document address: http://mp.weixin.qq.com/wiki/index.php? Title = % E8 % 87% AA % E5 % AE % 9A % E4 % B9 % 89% E8 % 8F % 9C % E5 % 8D % 95% E6 % 8E % A5 % E5 % 8F % a3
3. obtain the credential
3.1 obtain appid and appsecret
Find appid and appsecret in public platform> Advanced Functions> development mode.
{"Button": [{"name": "Public query", "sub_button": [{"type": "click", "name": "Weather query ", "key": "tianQi" },{ "type": "click", "name": "Bus query", "key": "gongJiao "}, {"type": "click", "name": "translation", "key": "fanYi"}] },{ "name": "Suzhou local ", "sub_button": [{"type": "click", "name": "Love Suzhou", "key": "loveSuzhou" },{ "type ": "click", "name": "Suzhou scenic spots", "key": "suzhouScenic" },{ "type": "click", "name": "Suzhou food ", "key": "suzhouFood" },{ "type": "click", "name": "living in Suzhou", "key": "liveSuzhou"}]}, {"type": "click", "name": "Contact Us", "key": "lianxiUs"}]}
Example:
Menu structure and description:
{"Button": [// button defines this structure as a menu {"name": "Main Menu name of the branch", "sub_button ": [// sub_button defines the sub-menu {"type": "click", // Button type "name": "branch sub-menu name 1", // menu name "key ": "loveSuzhou" // menu key value}, {"type": "click", "name": "branch sub-menu name 2", "key ": "liveSuzhou"}]}, // separated by commas (,) {"type": "click", "name": "Independent menu", "key ": "lianxiUs"}]}
Return description:
Correct Json return results:
{"errcode":0,"errmsg":"ok"}
Submit menu:
Use curl to submit the preceding menu data. the code is as follows:
$MENU_URL="https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$ACC_TOKEN;$ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $MENU_URL); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);curl_setopt($ch, CURLOPT_AUTOREFERER, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data);curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $info = curl_exec($ch);if (curl_errno($ch)) { echo 'Errno'.curl_error($ch);}curl_close($ch);var_dump($info);
Generate menu:
After the code for creating the menu is submitted to the server, it is not automatically generated. you need to execute the code file to generate the code. therefore, open the browser and enter the complete menu code URL in the address bar, the running result is as follows:
$MENU_URL="https://api.weixin.qq.com/cgi-bin/menu/get?access_token=".$ACC_TOKEN;$cu = curl_init();curl_setopt($cu, CURLOPT_URL, $MENU_URL);curl_setopt($cu, CURLOPT_RETURNTRANSFER, 1);$menu_json = curl_exec($cu);$menu = json_decode($menu_json);curl_close($cu);echo $menu_json;
Running result:
$ MENU_URL = "https://api.weixin.qq.com/cgi-bin/menu/delete? Access_token = ". $ ACC_TOKEN; $ cu = curl_init (); curl_setopt ($ cu, CURLOPT_URL, $ MENU_URL); curl_setopt ($ cu, CURLOPT_RETURNTRANSFER, 1); $ info = curl_exec ($ cu ); $ res = json_decode ($ info); curl_close ($ cu); if ($ res-> errcode = "0") {echo "menu deleted successfully ";} else {echo "menu deletion failed ";}
Running result:
For more articles on the development of custom menu functions on the public platform, please refer to the PHP Chinese website!