襄陽網站設計制作公司分享-php提交創建微信公眾號菜單
//創建菜單 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": "http://www.w6166.cn" }, { "name": "模板管理", "sub_button": [ { "type": "view", "name": "模板下載", "url": "http://www.w6166.cn/cates/lst/33.html" }, { "type": "view", "name": "免費模板", "url": "http://www.w6166.cn/cates/lst/34.html" } ] }, { "name": "新聞動態", "sub_button": [ { "type": "view", "name": "行業新聞", "url": "http://www.w6166.cn/cates/lst/19.html" }, { "type": "view", "name": "建站學堂", "url": "http://www.w6166.cn/cates/lst/20.html" }, { "type": "view", "name": "常見問題", "url": "http://www.w6166.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"}
關鍵詞: 襄陽網站設計制作 php提交創建微信公眾號菜單