微信公众号在开启了开发者自定义菜单后,公众号的菜单需要开发者写代码去管理菜单,下面给出结合官方文档所写的自定义菜单增删查改代码(php):

菜单创建或修改:

  1. <?php  
  2. /** 
  3.  * 微信公众平台-自定义菜单 创建菜单 
  4.  */  
  5.   
  6. header('Content-Type: text/html; charset=UTF-8');  
  7.   
  8. //替换为自己的appid和secret  
  9. $appid="wx9b4b97f6ce6647ef";  
  10. $secret="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";  
  11.   
  12. //获取access_token  
  13. $token_url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret;  
  14. $json=file_get_contents($token_url);  
  15. $res=json_decode($json);  
  16. $access_token=$res->access_token;  
  17.   
  18. //菜单数据,最多三个一级菜单button,一级菜单下面最多五个二级菜单sub_button  
  19. $data='{  
  20. "button":[  
  21.     {  
  22.         "name":"事件菜单",  
  23.         "sub_button":[  
  24.             {  
  25.                 "type":"click",  
  26.                 "name":"测试菜单1",  
  27.                 "key":"test1"  
  28.             },  
  29.             {  
  30.                 "type":"click",  
  31.                 "name":"测试菜单2",  
  32.                 "key":"test2"  
  33.             },  
  34.             {  
  35.                 "type":"click",  
  36.                 "name":"测试菜单3",  
  37.                 "key":"test3"  
  38.             }]  
  39.     },  
  40.     {  
  41.         "name":"链接菜单",  
  42.         "sub_button":[  
  43.             {  
  44.                 "type":"view",  
  45.                 "name":"移动网页开发",  
  46.                 "url":"http://www.karlew.com/?cat=16"  
  47.             },  
  48.             {  
  49.                 "type":"view",  
  50.                 "name":"公众平台开发",  
  51.                 "url":"http://www.karlew.com/?cat=5"  
  52.             },  
  53.             {  
  54.                 "type":"view",  
  55.                 "name":"前端性能优化",  
  56.                 "url":"http://www.karlew.com/?cat=39"  
  57.             }]  
  58.     },  
  59.     {  
  60.         "type":"view",  
  61.         "name":"开发笔记",  
  62.         "url":"http://www.karlew.com"  
  63.     }]  
  64. }';  
  65.   
  66. //POST方式请求  
  67. $menu_url="https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$access_token;  
  68. $ch = curl_init($menu_url);  
  69. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");  
  70. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);  
  71. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
  72. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen($data)));  
  73. $msg = json_decode(curl_exec($ch),true);  
  74.   
  75. if($msg['errcode'] == "0"){  
  76.     echo "创建成功";  
  77. }else{  
  78.     echo "创建失败";  
  79. }  
  80.   
  81. ?>  

 
菜单查询:

  1. <?php  
  2. /** 
  3.  * 微信公众平台-自定义菜单 查询菜单 
  4.  */  
  5.   
  6. header('Content-Type: text/html; charset=UTF-8');  
  7.   
  8. //替换为自己的appid和secret  
  9. $appid="wx9b4b97f6ce6647ef";  
  10. $secret="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";  
  11.   
  12. //获取access_token  
  13. $token_url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret;  
  14. $json=file_get_contents($token_url);  
  15. $res=json_decode($json);  
  16. $access_token=$res->access_token;  
  17.   
  18. //GET方式请求  
  19. $menu_url="https://api.weixin.qq.com/cgi-bin/menu/get?access_token=".$access_token;  
  20. $ch = curl_init();  
  21. curl_setopt($ch, CURLOPT_URL, $menu_url);  
  22. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
  23. $menu_json = json_decode(curl_exec($ch),true);  
  24. curl_close($ch);  
  25.   
  26. echo "<pre>";  
  27. print_r($menu_json);  
  28. echo "</pre>";  
  29.   
  30. ?>  

 

菜单删除:

  1. <?php  
  2. /** 
  3.  * 微信公众平台-自定义菜单 删除菜单 
  4.  */  
  5.   
  6. header('Content-Type: text/html; charset=UTF-8');  
  7.   
  8. //替换为自己的appid和secret  
  9. $appid="wx9b4b97f6ce6647ef";  
  10. $secret="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";  
  11.   
  12. //获取access_token  
  13. $token_url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret;  
  14. $json=file_get_contents($token_url);  
  15. $res=json_decode($json);  
  16. $access_token=$res->access_token;  
  17.   
  18. //GET方式请求  
  19. $menu_url="https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=".$access_token;  
  20. $ch = curl_init();  
  21. curl_setopt($ch, CURLOPT_URL, $menu_url);  
  22. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
  23. $msg = json_decode(curl_exec($ch),true);  
  24. curl_close($ch);  
  25.   
  26. if($msg['errcode'] == "0"){  
  27.     echo "删除成功";  
  28. }else{  
  29.     echo "删除失败";  
  30. }  
  31.   
  32. ?>  

 

以上已是完整代码,测试通过,复制即可,或在下面链接下载源码:

创建菜单:https://github.com/karlew/wechat/blob/master/menu/createMenu.php

查询菜单:https://github.com/karlew/wechat/blob/master/menu/getMenu.php

删除菜单:https://github.com/karlew/wechat/blob/master/menu/deleteMenu.php

欢迎留言