虽然,没有经过300元认证或微博关联认证的订阅号不能自定义菜单。但开发者可以申请测试账号。在测试账号里。开发者有所有高级功能接口权限。权限接口如下图所示:



因此,这里给了开发者一个体验高级接口的活路。

请按以下步骤,激活测试账号:
在PC上登陆你的账号,点击“功能” – “高级功能”,然后点击 “申请测试账号”。如下图所示。



后边的动作时要你输入手机号码,还有验证码这些。你输入了,然后提示说手机号码未注册(如果你真注册了那就没话说)。这个时候不要紧,请看登陆框左边那一个大大的二维码。拿起你的手机,登上你打微信,扫起来。这个时候你得点同意。然后你PC的网页会相应。这个时候,你就正在激活了测试账号。如下图所示:


测试账号的使用权限是一年,这一年中。足够你开房出很好的应用了。

好吧,下边进入正题。订阅号自定义菜单的实现。因为测试账号的申请成功,你拥有了appID和appsecret。现在,进行第一步,获取access_token。

http请求方式: GET
https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET

 

这些具体参数说明,返回说明,官方开发者文档里边有。这里就不详细叙说了,直接贴出php代码。

    define("APPID", "wx4ecdd26de9be3d55");
    define("APPSECRET", "216cc3721fc82f4f984e63f5c05b3155");

     public function GetAccessToken()
     {
         $appId = APPID;
         $secret = APPSECRET;
         $accessUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appId."&secret=".$secret;
         $r = file_get_contents($accessUrl);
         $r = json_decode($r, true);
         return $r['access_token'];
     }

 

有了access_token,那么就可以准备自定义菜单了。

接口调用请求说明

http请求方式:POST(请使用https协议) https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN

 

先定义菜单的结构,这里就用官方文档里的例子(具体菜单定义的细节要求,请看官方文档)。
然后以POST的形式,发送数据请求。如下边代码所示:

     public function Post()
     {
         $xjson = '{
             "button":[
             {
                  "type":"click",
                  "name":"今日歌曲",
                  "key":"V1001_TODAY_MUSIC"
              },
              {
                   "type":"click",
                   "name":"歌手简介",
                   "key":"V1001_TODAY_SINGER"
              },
              {
                   "name":"菜单",
                   "sub_button":[
                   {
                       "type":"view",
                       "name":"搜索",
                       "url":"http://www.soso.com/"
                    },
                    {
                       "type":"view",
                       "name":"视频",
                       "url":"http://v.qq.com/"
                    },
                    {
                       "type":"click",
                       "name":"赞一下我们",
                       "key":"V1001_GOOD"
                    }]
               }]
            }';
         $post_url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$this->GetAccessToken();
        $ch = curl_init($post_url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS,$xjson);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json',
        'Content-Length: ' . strlen($xjson))
        );
        $respose_data = curl_exec($ch);
        echo $respose_data;
     }

 

上边的这些就是自定义菜单的具体代码。只要以POST形式提交了就ok了。这个提交可以单独的提交和其他的验证自动回复等没有关系。提交后,如果是之前就已经关注过了你的公众号,24小时后菜单会生效。可以取消关注,再关注就可以马上生效了。

菜单的删减,修改可以进行覆盖提交。菜单目前有两种类型,click类型和view类型。需要在接受粉丝消息那里得到用户的操作行为,然会可以做出你自己想要的响应。下边贴出PHP完整的代码。

<?
    include 'com/vini123/fun/Menu.php'
    $menu = new Menu();
    $menu ->CreatMenu();
?>

 

Menu.php

<?php
define("APPID", "xxxxx");
define("APPSECRET", "xxxxx");
class Menu 
{
    public function Menu()
    {
            
    }
    
    private function getAccessToken()
    {
        $accessUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".APPID."&secret=".APPSECRET;
        $r = file_get_contents($accessUrl);
        $r = json_decode($r , true);
        $this ->writeStr($r);
        return $r['access_token'];
    }
    
    public function CreatMenu()
    {
        $menuJson = '{
                "button":[
                    {
                      "name":"今日音乐",
                      "sub_button":[
                        {
                          "type":"click",
                          "name":"《梦魇》",
                          "key":"V1001_MUSIC_1"
                        },
                        {
                          "type":"click",
                          "name":"《姐姐》",
                          "key":"V1001_MUSIC_2"
                        },
                        {
                          "type":"click",
                          "name":"Chorando Se Foi",
                          "key":"V1001_MUSIC_3"
                        },
                        {
                          "type":"click",
                          "name":"更多音乐",
                          "key":"V1001_MUSIC_4"
                         }
                      ]
                    },
                    {
                      "name":"美图欣赏",
                      "sub_button":[
                        {
                          "type":"click",
                          "name":"风小筝照片",
                          "key":"V1001_PHOTO_1"
                        },
                        {
                          "type":"click", 
                          "name":"享儿照片",
                          "key":"V1001_PHOTO_2"
                        },
                        {
                          "type":"click",
                          "name":"对面照片",
                          "key":"V1001_PHOTO_3"
                        },
                        {
                          "type":"click",
                          "name":"更多照片",
                          "key":"V1001_PHOTO_4"
                        }
                       ]
                    },
                    {
                      "name":"About Me",
                      "sub_button":[
                       { 
                          "type":"view",
                          "name":"我的技术博客",
                          "url":"http://blog1.vini123.com"
                       },
                       {
                          "type":"click",
                          "name":"音乐播放器",
                          "key":"V1001_ABOUT_1"
                       },
                       {
                          "type":"click",
                          "name":"杂志制作",
                          "key":"V1001_ABOUT_2"
                       },
                       {
                          "type":"click",
                          "name":"关于我",
                          "key":"V1001_ABOUT_3"
                       }
                      ]
                    }
                ]}';
        $post_url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$this->getAccessToken();
        $ch = curl_init($post_url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS,$menuJson);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json',
        'Content-Length: ' . strlen($menuJson))
        );
        $respose_data = curl_exec($ch);
        $this ->writeStr($menuJson + "\n");
        $this ->writeStr($respose_data);
        echo $respose_data;
        exit;
    }
    
    private function writeStr($str)
    {
        $date = date('Y-m-d');
        $open=fopen("log/".$date."_log.txt","a" );
        fwrite($open,$str);
        fclose($open);
    }
}
?>

 

最后,那么我贴出自定义菜单的截图来:


欢迎加我微信: vini1024