常見問題

襄陽網(wǎng)站設(shè)計(jì)制作公司分享-php提交創(chuàng)建微信公眾號(hào)菜單

常見問題

2510

字體:
 //創(chuàng)建菜單
    public function cateadd(){
        if (request()->isPost()){
            $data=input('code');//接收前端數(shù)據(jù)
            $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);//回調(diào)參數(shù)
            //判斷返回參數(shù)
            if($cate['errcode']==0){
                return json(['code' => 1, 'msg' => '創(chuàng)建菜單成功']);
            }else{
                return json(['code' => 0, 'msg' => '創(chuàng)建菜單失敗']);
            }
        }
        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;
    }

接口測(cè)試地址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://regardm.com"
        }, 
        {
            "name": "模板管理", 
            "sub_button": [
                {
                    "type": "view", 
                    "name": "模板下載", 
                    "url": "http://regardm.com/cates/lst/33.html"
                }, 
                {
                    "type": "view", 
                    "name": "免費(fèi)模板", 
                    "url": "http://regardm.com/cates/lst/34.html"
                }
            ]
        }, 
        {
            "name": "新聞動(dòng)態(tài)", 
            "sub_button": [
                {
                    "type": "view", 
                    "name": "行業(yè)新聞", 
                    "url": "http://regardm.com/cates/lst/19.html"
                }, 
                {
                    "type": "view", 
                    "name": "建站學(xué)堂", 
                    "url": "http://regardm.com/cates/lst/20.html"
                }, 
                {
                    "type": "view", 
                    "name": "常見問題", 
                    "url": "http://regardm.com/cates/lst/21.html"
                }
            ]
        }
    ]
}

參數(shù)說明


參數(shù)是否必須說明
button一級(jí)菜單數(shù)組,個(gè)數(shù)應(yīng)為1~3個(gè)
sub_button二級(jí)菜單數(shù)組,個(gè)數(shù)應(yīng)為1~5個(gè)
type菜單的響應(yīng)動(dòng)作類型,view表示網(wǎng)頁類型,click表示點(diǎn)擊類型,miniprogram表示小程序類型
name菜單標(biāo)題,不超過16個(gè)字節(jié),子菜單不超過60個(gè)字節(jié)
keyclick等點(diǎn)擊類型必須菜單KEY值,用于消息接口推送,不超過128字節(jié)
urlview、miniprogram類型必須網(wǎng)頁 鏈接,用戶點(diǎn)擊菜單可打開鏈接,不超過1024字節(jié)。 type為miniprogram時(shí),不支持小程序的老版本客戶端將打開本url。
media_idmedia_id類型和view_limited類型必須調(diào)用新增永久素材接口返回的合法media_id
appidminiprogram類型必須小程序的appid(僅認(rèn)證公眾號(hào)可配置)
pagepathminiprogram類型必須小程序的頁面路徑

返回結(jié)果

正確時(shí)的返回JSON數(shù)據(jù)包如下:

{"errcode":0,"errmsg":"ok"}

錯(cuò)誤時(shí)的返回JSON數(shù)據(jù)包如下(示例為無效菜單名長度):

{"errcode":40018,"errmsg":"invalid button name size"}


[聲明]原創(chuàng)不易,請(qǐng)轉(zhuǎn)發(fā)者備注下文章來源(hbsjsd.cn)【速建時(shí)代】。