The PHP public platform develops the custom menu function. [PHP public platform development series] 01. configure interface 02. public platform sample code analysis 03. subscribe processing 04. simple reply function development 05. weather forecast function [PHP public platform development series] 01. configuration interface
02. public platform sample code analysis
03. subscribe processing
04. simple reply function development
05. Development of the weather forecast function
06. development of translation functions
07. chatbot function development
08. Custom menu functions
Address: http://www.phpchina.com/archives/view-43404-1.html
This series is provided by PHPChina's special Author @ David _tang. For more information, see the author information and the address of this article. 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.
3.2 Use appid and appsecret to request access_token from the interface for obtaining Creden
Request address: https://api.weixin.qq.com/cgi-bin/token? Grant_type = client_credential & appid = APPID & secret = APPSECRET
Request parameter description:
Grant_type: obtain access_token and enter client_credential.
Appid: The unique credential of a third-party user
Secret: The unique credential key of a third-party user, which is appsecret
Return description:
Correct Json return results:
{"access_token":"ACCESS_TOKEN","expires_in":7200}
Response parameters:
Access_token: the obtained credential.
Expires_in: The validity period of the credential. unit: seconds.
3.3 Specific implementation
A. print out the format
The result is as follows:
B. get access_token
Note: The access_token corresponds to the public number, which is globally unique. repeated acquisition will invalidate the last access_token.
4. create a menu
Method:By posting a specific struct, you can create a custom menu on the client.
Request address: https://api.weixin.qq.com/cgi-bin/menu/create? Access_token = ACCESS_TOKEN
Request example:
{"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);
Running result:
Test results:
Menu created successfully.
5. query menu
Query the currently used custom menu structure.
Request address: https://api.weixin.qq.com/cgi-bin/menu/get? Access_token = ACCESS_TOKEN
The curl code 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:
The menu is queried successfully.
6. delete a menu
Cancels the currently used custom menu.
Request address: https://api.weixin.qq.com/cgi-bin/menu/delete? Access_token = ACCESS_TOKEN
The curl code is as follows:
$ 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:
Test results:
Menu deleted successfully.
01. configuration interface 02. public platform sample code analysis 03. subscribe processing 04. simple reply function development 05. weather forecast function...