小程序自定義菜單

以yii框架爲例

微信官方文檔

public function actionQrrr(){
        $APPID="wx3ea1*******148bc";
        $APPSECRET="7492106fcc*******f*******58d8a4e60";
        $url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$APPID."&secret=".$APPSECRET;
        $date=$this->postcurl($url);
        $access_token=$date['access_token'];
        $url="https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$access_token;
//        $url = 'https://'.$_SERVER['SERVER_NAME'];
        //type =  view
        $data = '{
    "button": [
        {
            "type": "miniprogram", 
            "name": "約課", 
            "url": "http://mp.weixin.qq.com", 
            "appid": "wx2ad7a428e00990ca", 
            "pagepath": "pages/index/index"
        }, 
        {
            "name": "發現", 
            "sub_button": [
                {
                    "type": "miniprogram", 
                    "name": "邀請有禮 ∣ 優惠券", 
                    "url": "http://mp.weixin.qq.com", 
                    "appid": "wx2ad7a428e00990ca", 
                    "pagepath": "pages/index/invite/invite?type=1"
                }
            ]
        }, 
        {
            "name": "我的", 
            "sub_button": [
                {
                    "type": "miniprogram", 
                    "name": "我的預約", 
                    "url": "http://mp.weixin.qq.com", 
                    "appid": "wx2ad7*****8e00990ca", 
                    "pagepath": "pages/my/myCourse/myCourse?type=1"
                }, //跳轉至小程序
                 {
                    "type": "scancode_push", 
                    "name": "上課掃碼", 
                    "key": "rselfmenu_1_0", 
                    "sub_button": [ ]
                },//打開掃碼
                 {
                    "type": "view",
                    "name": "邁小步", 
                    "url":"https://maibu.realizeidea.club/maibuweb/BabyStep.html"
                },//打開網站
                {
                    "type": "media_id",
                    "name": "聯繫我們", 
                    "media_id": "ZwEU4ZHPpv_BkwVNH5VdHE5vuYt6_HqdwjuDW2DUxAY"
                },//獲取素材
                {
                    "type": "view",
                    "name": "關於邁步", 
                    "url":"https://maibu.realizeidea.club/maibuweb/AboutUs.html"
                }
            ]
        }
    ]
}';
        $result=$this->postcurl($url,urldecode($data));
        return json_encode($result);
    }
    //請求接口方法
    function postcurl($url,$data = null)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        if (!empty($data)) {
            curl_setopt($ch, CURLOPT_POST, TRUE);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($ch);
        curl_close($ch);
        return $output = json_decode($output, true);
    }
    
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章