🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
**目录 (Table of Contents)** [TOC] ## 创建微信菜单 ```PHP // 我们现在添加3个微信一级菜单,每个一级菜单下添加两个二级菜单 // 菜单结构如下 // --- 团购 click // | -------- 团购简介 click 事件推送 // | -------- 团购预约 view 跳转 // --- 我的 click // | ------- 我的钱包 view 跳转 // | ------- 我的订单 view 跳转 // --- 购票 click // | ------ 预约购票 view 跳转 // | ------ 票务说明 view 跳转 $wxConfigure = new WechatConfig(); $wxConfigure->setParameter("appid",YoursAppId ); $wxConfigure->setParameter("appsecret",YoursSecret); $wxConfigure->setParameter("access_token",ACCESS_TOKEN); $menuApi = new MenuApi($wxConfigure); // 添加 团购一级菜单 $fristMenu = new ClickMenu("团购"); // 添加 团购简介 和 团购预约 二级菜单 $fristMenu->addChild(new ClickMenu("团购简介","groupbuy info")) ->addChild(new ViewMenu("团购预约","https://github.com/DarthAhsoka/Endor-Wechat")); // 添加 我的一级菜单 $secondMenu = new ClickMenu("我的"); $secondMenu->addChild(new ViewMenu("我的钱包","https://github.com/DarthAhsoka/Endor-Wechat")) ->addChild(new ViewMenu("我的订单","https://github.com/DarthAhsoka/Endor-Wechat")); // 添加 购票一级菜单 $thirdMenu = new ClickMenu("购票"); // 也可以不使用连贯操作 $markTicket = new ViewMenu("预约购票"); $markTicket->setUrl("https://github.com/DarthAhsoka/Endor-Wechat"); $thirdMenu->addChild($markTicket); $ticketInfo = new ViewMenu("票务说明"); $ticketInfo->setUrl("https://github.com/DarthAhsoka/Endor-Wechat"); $thirdMenu->addChild($ticketInfo); $menuApi = new MenuApi($wxConfigure); $menuApi->addMenuItem($fristMenu); $menuApi->addMenuItem($secondMenu); $menuApi->addMenuItem($thirdMenu); // 创建菜单,调用函数后,将修改微信公众号上的菜单 try { $menuApi->create(); } catch(WechatException $e) { echo $e->errorMessage(); } ``` ## 从微信公众号服务器上获取菜单,并遍历,识别菜单类型和名字 ```PHP $wxConfigure = new WechatConfig(); $wxConfigure->setParameter("appid",YoursAppId ); $wxConfigure->setParameter("appsecret",YoursSecret); $wxConfigure->setParameter("access_token",ACCESS_TOKEN); $menuApi = new MenuApi($wxConfigure); try { $ret = $menuApi->get(); if($ret) { foreach ($menuArr as $v) { // 这里获取一级菜单,getName获取到菜单名字,getType可获取菜单类型 echo $v->getName() . " (" .$v->getType() . " )" ; echo "\r\n"; $children = $v->getChildren(); foreach($children as $n) { // 这里获取二级菜单, echo "\t\t".$n->getName() . " (" .$n->getType() . " )" ; echo "\r\n"; } } } } catch(WechatException $e) { echo $e->errorMessage(); } ``` ## 删除所有微信菜单 ```PHP $wxConfigure = new WechatConfig(); $wxConfigure->setParameter("appid",YoursAppId ); $wxConfigure->setParameter("appsecret",YoursSecret); $wxConfigure->setParameter("access_token",ACCESS_TOKEN); $menuApi = new MenuApi($wxConfigure); $menuApi->delete(); ```