文章来源(hbsjsd.cn)湖北高端网站定制开发公司-速建时代
//创建菜单 public function cateadd(){ if (request()->isPost()){ $data=input('code');//接收前端数据 $accessToken=$Weixin->access_token($this->appid,$this->secret);//获取access_token $urlfb="https://api.weixin.qq.com/cgi-bin/menu/create?access_token=$accessToken";//提前菜单地址 $result =$this-> https_request($urlfb, $data);//https_request提交方式 $cate = json_decode($result, true);//回调参数 //判断返回参数 if($cate['errcode']==0){ return json(['code' => 1, 'msg' => '创建菜单成功']); }else{ return json(['code' => 0, 'msg' => '创建菜单失败']); } } return view(); } //提交方式 public function https_request($url,$data = null){ $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); if (!empty($data)){ curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); } curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($curl); curl_close($curl); return $output; }
接口测试地址https://mp.weixin.qq.com/debug/cgi-bin/apiinfo?t=index&type=%E8%87%AA%E5%AE%9A%E4%B9%89%E8%8F%9C%E5%8D%95&form=%E8%87%AA%E5%AE%9A%E4%B9%89%E8%8F%9C%E5%8D%95%E5%88%9B%E5%BB%BA%E6%8E%A5%E5%8F%A3%20/menu/creat
{ "button": [ { "type": "view", "name": "首页", "url": "https://www.hbsjsd.cn" }, { "name": "模板管理", "sub_button": [ { "type": "view", "name": "模板下载", "url": "https://www.hbsjsd.cn/cates/lst/33.html" }, { "type": "view", "name": "免费模板", "url": "https://www.hbsjsd.cn/cates/lst/34.html" } ] }, { "name": "新闻动态", "sub_button": [ { "type": "view", "name": "行业新闻", "url": "https://www.hbsjsd.cn/cates/lst/19.html" }, { "type": "view", "name": "建站学堂", "url": "https://www.hbsjsd.cn/cates/lst/20.html" }, { "type": "view", "name": "常见问题", "url": "https://www.hbsjsd.cn/cates/lst/21.html" } ] } ] }
参数 | 是否必须 | 说明 |
---|---|---|
button | 是 | 一级菜单数组,个数应为1~3个 |
sub_button | 否 | 二级菜单数组,个数应为1~5个 |
type | 是 | 菜单的响应动作类型,view表示网页类型,click表示点击类型,miniprogram表示小程序类型 |
name | 是 | 菜单标题,不超过16个字节,子菜单不超过60个字节 |
key | click等点击类型必须 | 菜单KEY值,用于消息接口推送,不超过128字节 |
url | view、miniprogram类型必须 | 网页 链接,用户点击菜单可打开链接,不超过1024字节。 type为miniprogram时,不支持小程序的老版本客户端将打开本url。 |
media_id | media_id类型和view_limited类型必须 | 调用新增永久素材接口返回的合法media_id |
appid | miniprogram类型必须 | 小程序的appid(仅认证公众号可配置) |
pagepath | miniprogram类型必须 | 小程序的页面路径 |
返回结果
正确时的返回JSON数据包如下:
{"errcode":0,"errmsg":"ok"}
错误时的返回JSON数据包如下(示例为无效菜单名长度):
{"errcode":40018,"errmsg":"invalid button name size"}
[声明]原创不易,请转发者备注下文章来源(hbsjsd.cn)【速建时代】。