Home > Article > Backend Development > PHP WeChat development: How to implement a custom menu
With the rapid development of WeChat official accounts, more and more companies are beginning to use WeChat official accounts for marketing and user services. In the development of WeChat public accounts, custom menus are a very important function. This article will introduce how to implement a custom menu in PHP WeChat development.
1. Prerequisites
Before you start, you need to prepare the following things:
1. The appid and appsecret of the WeChat official account.
2. The WeChat official account has been bound to WeChat Pay and has enabled JSAPI payment permissions. (JSAPI payment is required for custom menus)
2. Create menus
Before you start creating menus, you need to understand the rules of WeChat custom menus.
1. The custom menu can include up to 3 first-level menus, and each first-level menu can include up to 5 second-level menus.
2. The menu control authority is the public account, and operations such as creation, query, and deletion of menus can be implemented through the interface.
3. The response action types of the menu include click (click push event), view (jump URL), scancode_push (scan code push event), scancode_waitmsg (scan code push event and pop up "message receiving" prompt box), pic_sysphoto (pops up the system to take pictures and send pictures), pic_photo_or_album (pops up to take photos or albums to send pictures), pic_weixin (pops up the WeChat album sender), location_select (pops up the geographical location selector), media_id (sends messages), view_limited ( Jump to image and text message URL).
Next, we start creating a custom menu. First, you need to define the corresponding menu structure. The sample code is as follows:
class MenuButton{ public $type;//菜单类型 public $name;//菜单名称 public $key;//菜单key public $url;//菜单url public $sub_button;//二级菜单数组 public function __construct($name,$type,$key=null,$url=null,$sub_button=null){ $this->name = $name; $this->type = $type; $this->key = $key; $this->url = $url; if(!empty($sub_button)){ $this->sub_button = $sub_button; } } public function toArray(){ $arr = array(); if(count($this->sub_button) > 0){ foreach($this->sub_button as $button){ array_push($arr,$button->toArray()); } } $data = array(); $data['name'] = urlencode($this->name); switch($this->type){ case 'click': $data['type'] = $this->type; $data['key'] = $this->key; break; case 'view': $data['type'] = $this->type; $data['url'] = $this->url; break; case 'scancode_push': case 'scancode_waitmsg': case 'pic_sysphoto': case 'pic_photo_or_album': case 'pic_weixin': case 'location_select': $data['type'] = $this->type; $data['key'] = $this->key; break; case 'media_id': case 'view_limited': $data['type'] = $this->type; $data['media_id'] = $this->key; break; } if(count($arr) > 0){ $data['sub_button'] = $arr; } return $data; } }
This defines a MenuButton structure, including the first-level menu and the second-level menu, and also includes the menu type, name, key, url etc. When instantiating a MenuButton object, you can pass in the corresponding parameters.
Next, we create a custom menu through the MenuButton structure. The sample code is as follows:
function create_menu($access_token){ $menu = array(); $menu[] = new MenuButton('一级菜单1','click','click1'); $menu[] = new MenuButton('一级菜单2','view',null,'http://www.baidu.com'); $menu[] = new MenuButton('一级菜单3',null,null,null,array( new MenuButton('二级菜单1','scancode_push','scancode_push1'), new MenuButton('二级菜单2','scancode_waitmsg','scancode_waitmsg1'), new MenuButton('二级菜单3','pic_sysphoto','pic_sysphoto1'), new MenuButton('二级菜单4','pic_photo_or_album','pic_photo_or_album1') )); $data = array(); $data['button'] = array(); foreach($menu as $button){ array_push($data['button'],$button->toArray()); } $url = 'https://api.weixin.qq.com/cgi-bin/menu/create?access_token='.$access_token; $result = curl_post($url,urldecode(json_encode($data))); return $result; }
Here, by creating an instance of MenuButton, the first-level menu and the second-level menu are defined layer by layer. Finally, Through the corresponding interface, the menu is created successfully.
3. Query menu
In addition to creating menus, we can also use the interface to query created menus. The sample code is as follows:
function menu_query($access_token){ $url = 'https://api.weixin.qq.com/cgi-bin/menu/get?access_token='.$access_token; $result = curl_get($url); return $result; }
The menu_get interface is used here to query the created menu.
4. Delete menu
If we need to delete the existing menu, we can also operate it through the corresponding interface. The sample code is as follows:
function menu_delete($access_token){ $url = 'https://api.weixin.qq.com/cgi-bin/menu/delete?access_token='.$access_token; $result = curl_get($url); return $result; }
5. Summary
This article introduces how to create, query and delete custom menus in PHP WeChat development. I hope it will be helpful to everyone. At the same time, you need to pay attention to the rules of custom menus and define and create menus according to the rules. Customized menus can help companies better serve users and improve user experience.
The above is the detailed content of PHP WeChat development: How to implement a custom menu. For more information, please follow other related articles on the PHP Chinese website!