一段简单的自定义微信公众号菜单代码

来源:互联网 发布:java直接读取zip文件 编辑:程序博客网 时间:2024/06/05 02:08

$appId = $_POST['appId'];
    $appSecret = $_POST['appSecret'];


        $get_url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='+$appId+'&secret='+$appSecret;
        $get_return = file_get_contents($get_url);
        $get_return = (array)json_decode($get_return);
        if( !isset($get_return['access_token']) ){exit( '获取access_token失败!' );}

$ejson = '{ 
                     "button":[
                         {
                               "name":"篮球",
                               "sub_button":[
                                    {
                                       "type":"click",
                                       "name":"nba",
                                       "key":"V1001_NBA"
                                    },
                                    {
                                       "type":"click",
                                       "name":"cba",
                                       "key":"V1001_CBA"
                                    }
                                ]
                           },
                           {
                               "name":"体育",
                               "sub_button":[
                                    {
                                       "type":"click",
                                       "name":"足球",
                                       "key":"V1001_ZUQIU"
                                    },
                                    {
                                       "type":"click",
                                       "name":"排球",
                                       "key":"V1001_PAIQIU"
                                    },
                                    {
                                       "type":"click",
                                       "name":"台球",
                                       "key":"V1001_TAIQIU"
                                    }
                                ]
                           },
                           {
                               "name":"新闻",
                               "sub_button":[
                                    {
                                       "type":"click",
                                       "name":"国内新闻",
                                       "key":"V1001_GNNEWS"
                                    },
                                    {
                                       "type":"click",
                                       "name":"国际新闻",
                                       "key":"V1001_GJNEWS"
                                    }
                                ]
                           }
                       ]
                    }';


        $post_url = 'https://api.weixin.qq.com/cgi-bin/menu/create?access_token='.$get_return['access_token'];
        $ch = curl_init($post_url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS,$ejson);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json',
        'Content-Length: ' . strlen($ejson))
        );
        $respose_data = curl_exec($ch);
        echo $respose_data;
        exit;



0 0