Provides various official and user-released code examples. For code reference, you are welcome to exchange and use them after learning and testing. Although only one view type is available, you can add your own judgments for other types, here is a reference case, so there are still some judgment procedures that do not have time to be added.
The data table is uploaded for learning only. The full version updated later will be placed in the [slackck] background system. The full site source code can be downloaded from the QQ group: 457295993 files.
The Code is as follows:
/*
* Generate a custom menu
*/
Public function we_menu_make (){
$ We = M ('sys ')-> where (array ('sys _ id' => 1)-> find (); // read configuration parameters
$ Url = "https://api.weixin.qq.com/cgi-bin/token? Grant_type = client_credential & appid = ". $ we [" wesys_appid "]." & secret = ". $ we [" wesys_appsecret "]." ";
$ Ch = curl_init (); // Initialization
Curl_setopt ($ ch, CURLOPT_URL, $ url );
Curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, FALSE );
Curl_setopt ($ ch, CURLOPT_SSL_VERIFYHOST, FALSE );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
$ Output = curl_exec ($ ch );
Curl_close ($ ch );
$ Jsoninfo = json_decode ($ output, true );
$ Access_token = $ jsoninfo ['Access _ token'];
/*
* Start Menu Processing
* Only three pieces of leftid = 0 are retrieved.
*/
$ We_menu = M ('We _ menu ')-> where (array ('We _ menu_leftid' => 0, 'we _ menu_open '=> 1 )) -> order ('We _ menu_order ')-> limit (3)-> select ();
/*
* Menu Data Reorganization
* For the restructuring structure, refer to the public platform development documentation.
* Name menu name
* Type menu type
* Url: For view type
*/
$ Data = '{"button": ['; // menu Header
Foreach ($ we_menu as $ v ){
$ Data. = '{"name": "'. $ v ['we _ menu_name '].'", '; // menu name
$ Count = M ('We _ menu ')-> where (array ('We _ menu_leftid' => $ v ['we _ menu_id '], 'We _ menu_open '=> 1)-> limit (5)-> order ('We _ menu_order')-> count (); // determines whether a subtopic exists.
If ($ count) {// second-level column
$ Data. = '"sub_button ":[';
$ We_twomenu = M ('We _ menu ')-> where (array ('We _ menu_leftid' => $ v ['we _ menu_id '], 'We _ menu_open '=> 1)-> order ('We _ menu_order')-> limit (5)-> select ();
$ K = 0;
Foreach ($ we_twomenu as $ t ){
$ K = $ k + 1;
$ Data. = '{"name": "'. $ t ['we _ menu_name '].'", ';
$ Data. = '"type": "view ",';
$ Data. = '"url": "http://www.baidu.com "';
If ($ k = $ count ){
$ Data. = '}';
} Else {
$ Data. = '},';
}
}
$ Data. = ']},';
} Else {
$ Data. = '"type": "view ",';
$ Data. = '"url": "http://www.baidu.com "';
}
}
$ Data. = '},]';
$ Data. = '}';
$ Url = "https://api.weixin.qq.com/cgi-bin/menu/create? Access_token = $ access_token ";
$ Ch = curl_init (); // Initialization
Curl_setopt ($ ch, CURLOPT_URL, $ url );
Curl_setopt ($ ch, CURLOPT_POST, 1 );
Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ data );
Curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, FALSE );
Curl_setopt ($ ch, CURLOPT_SSL_VERIFYHOST, FALSE );
$ This-> success ('menu generated successfully', U ('We _ call'), 1 );
Curl_exec ($ ch );
Curl_close ($ ch );
Return $ access_token;
}
Mr_we_menu.rar (911 B download: 66 times)
AD: truly free, domain name + VM + enterprise mailbox = 0 RMB