"PHP Public Platform Development Series"
01. Configure the interface
02. Public Platform Sample Code analysis
03. Subscription event (SUBSCRIBE) processing
04. Simple Reply Function development
05. Development of weather forecast function
06. Translation Function Development
07. Chat Robot Function Development
08. Custom Menu Functions
This address: http://www.phpchina.com/archives/view-43404-1.html
This series by Phpchina invited author @david_tang feeds, reprint please indicate the author information and this article address.
First, Introduction
The public platform service number and the previous subscription number that successfully applied for beta eligibility have the ability to customize the menu. Developers can use this feature to add a custom menu to the bottom of the session interface of the public account, and the user can click on the options in the menu to bring up the corresponding reply message or Web link. The custom menu interface will provide more possibilities for the information display space of the public account. This article will do a simple development application for the custom menu for the reader's reference.
II. Official Notes
After the developer obtains the voucher, the user can use the voucher to create, query and delete the custom menu of the public account. The custom menu interface implements the following types of buttons:
Click (Tap Event):
After the user clicks the Click type button, the server pushes the click event to the developer via the Message Interface (event type), and with the key value entered by the developer in the button, the developer can reply to the message via a custom key value.
View (Visit Web page):
When the user clicks the View Type button, it jumps directly to the URL specified by the developer.
After you create a custom menu, it takes 24 hours for clients to show up due to client-side caching. It is recommended that you try to cancel your attention to the public account after testing, and then you can see the effect after the 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
Third, obtain the use voucher
3.1 Getting AppID and Appsecret
Find AppID and Appsecret in the public Platform > Advanced Features > Development model.
3.2 Get interface requests to credentials using AppID and Appsecret Access_token
Request Address: Https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
Request Parameter Description:
Grant_type: Get Access_token fill in client_credential
AppID: Third-party user unique credentials
Secret: Third-party user unique credential key, both Appsecret
Return Description:
The correct JSON returns the result:
{"Access_token": "Access_token", "expires_in": 7200}
Return parameter Description:
Access_token: Access to the voucher
EXPIRES_IN: Voucher valid time, Unit: SEC
3.3 Concrete Implementations
A. Print out the format
The results are as follows:
B. Get Access_token
Note: Access_token corresponding to the public number is a globally unique ticket, and a duplicate fetch will result in the last acquired Access_token failure.
Iv. Creating a Menu
method: Create a custom menu on the client by post a specific struct.
Request Address: Https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN
Sample Request:
{"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 attractions ", " key ":" Suzhouscenic " }, { " type ":" Click ", " name ":" Suzhou Gourmet ", " key ":" Suzhoufood " }, { " type ":" Click ", " name ":" Live in Suzhou ", " key ":" Livesuzhou " }] }, { "type": "Click", "name": "Contact Us", "key": "Lianxius" }]}
Example Description:
Menu structure and Description:
{"button": [ //button defines the structure as a menu { "name": "Branch main course sole name", "Sub_button": [//sub_button Define submenu { " Type ":" click ",//Button type " name ":" Branch submenu name 1 ",//Menu name " key ":" Lovesuzhou "//Menu key value }, { " type ":" Click ", " "Name": "Branch submenu Name 2", "key": "Livesuzhou" }] }, and//between menus, separating { "type": "Click", "name": "Standalone Menu", "key": "Lianxius" }]}
Return Description:
The correct JSON returns the result:
{"Errcode": 0, "errmsg": "OK"}
Submit Menu:
Submit the above menu data via curl, with the following code:
$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);
Operation Result:
Test results:
The menu was created successfully.
Five, Query menu
Queries 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;
Operation Result:
The menu query was successful.
Vi. Deleting menus
Cancels a custom menu that is currently in use.
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 delete failed";}
Operation Result:
Test results:
Menu deleted successfully.
http://www.bkjia.com/PHPjc/742438.html www.bkjia.com true http://www.bkjia.com/PHPjc/742438.html techarticle "PHP Public Platform Development Series" 01. Configure interface 02. Public Platform Sample Code Analysis 03. Subscription events (subscribe) processing 04. Simple reply Function Development 05. Weather Forecast Function ...