微信开发之自定义菜单 + php

微信官方文档 https://developers.weixin.qq.com/doc/offiaccount/Custom_Menus/Creating_Custom-Defined_Menu.html

php代码

    // 自定义菜单
    public function menu(){
        // 实例化微信操作类
        $wx = new Wechat();
        
        $token = $wx->getAccessToken();
        
        $jsonmenu = '{ 
          "button":[ 
          { 
           "name":"天气预报", 
           "sub_button":[ 
           { 
            "type":"click", 
            "name":"北京天气", 
            "key":"天气北京"
           }, 
           { 
            "type":"click", 
            "name":"上海天气", 
            "key":"天气上海"
           }, 
           { 
            "type":"click", 
            "name":"广州天气", 
            "key":"天气广州"
           }, 
           { 
            "type":"click", 
            "name":"深圳天气", 
            "key":"天气深圳"
           }, 
           { 
            "type":"view", 
            "name":"本地天气", 
            "url":"http://m.hao123.com/a/tianqi"
           }] 
           
          
          }, 
          { 
           "name":"瑞雪", 
           "sub_button":[ 
           { 
            "type":"click", 
            "name":"公司简介", 
            "key":"company"
           }, 
           { 
            "type":"click", 
            "name":"趣味游戏", 
            "key":"游戏"
           }, 
           { 
            "type":"click", 
            "name":"讲个笑话", 
            "key":"笑话"
           }] 
          }] 
        }'; 
        
        $url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=$token";
        // 生成自定义菜单
        $res = $wx->http_curl($url, 'post', 'json', $jsonmenu);
        echo "<pre>";
        print_r($res);
    }

效果图

 

 

 Wechat类是我自己封装的,比如获取access_token,ticket,curl请求这些,代码可以看上一篇文字 微信开发之生成二维码,扫码关注公众号PHP

思路说明:

1.公众号配置:JS安全域名,IP白名单

2.获取AccessToken,过期时间默认为7200秒

3.生成菜单:携带AccessToken和自定义菜单内容,并调用生成菜单接口(需注意拼接菜单格式)

posted @ 2020-04-22 11:06  下页、再停留  阅读(440)  评论(0编辑  收藏  举报