微信公众平台开发自定义菜单的教程,微信公众平台自定义菜单开发示例

脚下Wechat服务号自定蒿子单最多包涵3个顶尖菜单,每种一级菜单最多带有5个二级菜单。拔尖菜单最多4个汉字,二级菜单最多7个汉字,多出来的有的将会以“…”代替。请在乎,创制自定同蒿单后,由于Wechat客商端缓存,须求24小时Wechat客商端才会突显出来。建议测验时能够品味撤除关心民众账号后再行关切,则能够看来创立后的功力。

风度翩翩、自定蒿菜单概述

 TencentWechat3月18日规范颁发大伙儿平台自定蓬花菜单功能。近年来该意义仅提必要合营社和机构,且内测资格须求报名。对于那生机勃勃新扩展效益,目前仅某个账号已经开展。至此,Wechat的商业化价值被越来越挖掘出来。

当下自定菊花菜单接口可达成两种档案的次序按键,如下:click:客商点击click类型开关后,Wechat服务器会经过音信接口推送新闻类型为event的组织给开垦者,并且带上按键中开采者填写的key值,开荒者能够由此自定义的key值与客户举办互相;view:客户点击view类型按键后,Wechat顾客端将会张开开荒者在按键中填入的url值,达到展开网页的目标,建议与网页授权获取客商大旨音信接口结合,获得客商的登八个人音信。

自定蒿菜单能够支持公众号加多分界面,让客商越来越好越来越快地知道民众号的机能。开启自定义菜单后,大伙儿号分界面如图所示:

 

接口调用伏乞表达http诉求格局:POST

图片 1

图片 2

 { "button":[ {"type":"click","name":"我的信息","sub_button":[ {"type":"click","name":"拇指查询","key":"BUTTON_1"}, {"type":"click","name":"拇指请假","key":"BUTTON_2"}, {"type":"view","name":"工号绑定","url":"http://XXXXXXXXXXXXXXXXX"}] }, {"type":"click","name":"业务流程","key":"BUTTON_3"}, {"name":"员工建议","sub_button":[ {"type":"view","name":"思想火花","url":"http://XXXXXXXXXXXXXXXXXX"}, {"type":"view","name":"奖品兑换","url":"http://XXXXXXXXXXXXXXXXXX"}, {"type":"click","name":"赞一下我们","key":"BUTTON_ZAN"}] } ]}

二、申请自定菊花菜单

 

回去结果正确时的回到JSON数据包如下:{“errcode”:0,”errmsg”:”ok”}错误时的回来JSON数据包如下:{“errcode”:40018,”errmsg”:”invalid
button name size”}

私家订阅号使用博客园证实、集团订阅号通过微信阐明;能够报名到自定桐花菜单资格

   开辟者可应用该意义为公众账号的对话界面尾部扩展自定菊花菜单。顾客点击菜单中的选项,能够调出相应的过来音信或网页链接。

     access_token; //创建菜单 $url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=$token"; $jsonData = file_get_contents; $ch = curl_init; curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt; curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData); curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); $output = curl_exec; var_dump; ?> 

{ "button":[ {"type":"click","name":"我的信息","sub_button":[ {"type":"click","name":"拇指查询","key":"BUTTON_1"}, {"type":"click","name":"拇指请假","key":"BUTTON_2"}, {"type":"view","name":"工号绑定","url":"http://XXXXXXXXXXXXXXXXX"}] }, {"type":"click","name":"业务流程","key":"BUTTON_3"}, {"name":"员工建议","sub_button":[ {"type":"view","name":"思想火花","url":"http://XXXXXXXXXXXXXXXXXX"}, {"type":"view","name":"奖品兑换","url":"http://XXXXXXXXXXXXXXXXXX"}, {"type":"click","name":"赞一下我们","key":"BUTTON_ZAN"}] } ]}

 $wechatObj = new wechatCallbackAPI(); if (isset { $wechatObj->valid(); } else { $wechatObj->responseMsg(); } class wechatCallbackAPI { private $token = "WEIXIN"; private $appId = "APPID"; private $appSecret = "APPSECRET"; private function checkSignature() { $signature = $_GET["signature"]; $timestamp = $_GET["timestamp"]; $nonce = $_GET["nonce"]; $tmpArr = array($this->token, $timestamp, $nonce); sort; $tmpStr = implode; $tmpStr = sha1; if($tmpStr == $signature) { return true; } else { return false; } } private function getAccessToken() { $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appId&secret=$this->appSecret"; $ch = curl_init; $curl_setopt($ch, CURLOPT_HEADER, 0); $curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $curl_setopt; $curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); $curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); $output = curl_exec; if  { return ""; } $result = json_decode; return $result->access_token; } public function valid() { $echoStr = $_GET["echostr"]; //valid signature, option if($this->checkSignature{ echo $echoStr; exit; } } public function responseMsg() { //get post data, May be due to the different environments $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; if { echo ""; exit; } //extract post data $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $fromUsername = $postObj->FromUserName; $toUsername = $postObj->ToUserName; $time = time(); //文本消息模板 $textTpl = "   %s   0 "; switch (strtolower(trim { case "text": //文本消息 $keyword = trim; if { $msgType = "text"; $contentStr = "$fromUsername, 您发送了文本信息: $keyword "; if  == "time") { $contentStr = date; } $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); } else { $resultStr = "Input something..."; } break; case "image": //图片消息 $msgType = "text"; $contentStr = "$fromUsername, 您发送了图片信息"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); break; case "voice": //声音消息 $msgType = "text"; $contentStr = "$fromUsername, 您发送了声音信息"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); break; case "video": //视频消息 $msgType = "text"; $contentStr = "$fromUsername, 您发送了视频信息"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); break; case "location": //位置消息 $msgType = "text"; $contentStr = "$fromUsername, 您发送了位置信息"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); break; case "link": //链接消息 $msgType = "text"; $contentStr = "$fromUsername, 您发送了链接信息"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); break; case "event": //事件 switch (strtolower)) { case "subscribe": //关注事件 $msgType = "text"; $contentStr = "欢迎您关注XXXXXXX"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); break; case "unsubscribe": //取消关注事件 break; case "scan": //用户已关注时扫描二维码事件 $msgType = "text"; $contentStr = "$fromUsername, 您扫描了二维码"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); break; case "location": //上传地理位置事件 $msgType = "text"; $contentStr = "$fromUsername, 您上传地理位置"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); break; case "click": //自定义菜单事件 $msgType = "text"; $contentStr = "$fromUsername, 您点击了自定义菜单 $postObj->EventKey "; if ("BUTTON_ZAN" == $postObj->EventKey) { $contentStr = "感谢您的赞,我们会继续提供更优质的服务。"; } $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); ; break; default: $resultStr = ""; } break; default: $resultStr = ""; } echo $resultStr; } }?>

劳动号私下认可有菜单权限。

美食指南创制 接口表明
经过POST二个特定构造体,实今后Wechat客商端成立自定同蒿单。
恳请说明
http央求方式:POST

自定菊花菜单查询

三、获得AppId 和AppSecert

https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN

采用接口创制自定桐花菜单后,开垦者还可利用接口查询自定蒿子杆单的布局。

在Wechat后台使用找到高等功效-开采情势

构造体其实正是两个json格式的字符串,相信广大有恋人就卡在这里边了,怎么往那个接口地址
post 三个构造体呢,实今世码如下:

伏乞表明http央求格局:GET

图片 3

 代码如下

回到表达对应创设接口,准确的Json重临结果:复制代码
代码如下:{“menu”:{“button”:[{“name”:”小编的音讯”,”sub_button”:[{“type”:”click”,”name”:”拇指查询”,”key”:”BUTTON_1″,”sub_button”:[]},{“type”:”click”,”name”:”拇指请假”,”key”:”BUTTON_2″,”sub_button”:[]},{“type”:”view”,”name”:”工号绑定”,”url”:”http://XXXXXXXX”,”sub_button”:[]}]},{“type”:”click”,”name”:”业务流程”,”key”:”BUTTON_3″,”sub_button”:[]},{“name”:”工作者提出”,”sub_button”:[{“type”:”view”,”name”:”理念火花”,”url”:”http://XXXXXXXX”,”sub_button”:[]},{“type”:”view”,”name”:”奖品兑换”,”url”:”http://XXXXXXXX”,”sub_button”:[]},{“type”:”click”,”name”:”赞一下我们”,”key”:”BUTTON_ZAN”,”sub_button”:[]}]}]}}

化为开垦者之后,就能够见到appid和appsecert了

public static void main(String[] args) throws Exception {
    URL url = new URL(“
                                 /menu/create?access_token=TOKEN”);
        String responeJsonStr = “{“+
                                     “”button”:[“+
                                     “{“name”:”饭馆预约”,”+
                                     “”sub_button”:[“+
                             “{“+
                                “”type”:”click”,”+
                                “”name”:”周边旅舍”,”+
                                “”key”:”jintoneinn488878-1″”+
                            “},”+
                            “{“+
                                “”type”:”click”,”+
                                “”name”:”查找旅馆”,”+
                                “”key”:”jintoneinn488878-2″”+
                            “}”+
                        “]”+
                    “}”
                “]”+
            “}”;
        HttpURLConnection conn = (HttpURLConnection)
url.openConnection();
        conn.setRequestMethod(“POST”);
        conn.setDoOutput(true);
        conn.connect();
       
conn.getOutputStream().write(responeJsonStr.getBytes(“utf-8”));
        InputStream is = conn.getInputStream();
        BufferedReader in = new BufferedReader(new
InputStreamReader(is,”utf-8″));
        StringBuffer buffer = new StringBuffer();
        String line = “”;
        while ((line = in.readLine()) != null) {
            buffer.append(line);
        }
        System.out.println(buffer.toString()); 
    }

自定蒿子单删除

图片 4

菜谱查询 接口表明

利用接口创设自定同蒿单后,开辟者还可选取接口删除当前接收的自定桐花菜单。

假使未有url和Token , 你能够先用方倍专门的工作室的底下的测量检验通过

查询当前使用的自定菊花菜单构造。

央浼表达http央浼情势:GET

URL:   
Token:  weixin

央浼表达

回来注解对应创制接口,准确的Json重返结果:{“errcode”:0,”errmsg”:”ok”}

四、获得Access Token

http央浼形式:GET

以上正是本文的全体内容,希望对大家的上学抱有利于,也愿意我们不吝赐教脚本之家。

用appid和appsecert获得access token,接口为

归来评释

对应成立接口,精确的Json重临结果:

程序达成如下

 代码如下

$appid = “”;
$appsecret = “”;
$url =
“”;
 
$ch = curl_init();
curl_setopt($ch,
CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
$jsoninfo = json_decode($output, true);
$access_token = $jsoninfo[“access_token”];
你也得以一向在浏览器地方栏中,拼接出地址,实施后,得到如下数据

{“menu”:{“button”:[{“type”:”click”,”name”:”前几天歌曲”,”key”:”V1001_TODAY_MUSIC”,”sub_button”:[]},{“type”:”click”,”name”:”明星简单介绍”,”key”:”V1001_TODAY_SINGER”,”sub_button”:[]},{“name”:”菜单”,”sub_button”:[{“type”:”click”,”name”:”hello
word”,”key”:”V1001_HELLO_WORLD”,”sub_button”:[]},{“type”:”click”,”name”:”赞一下大家”,”key”:”V1001_GOOD”,”sub_button”:[]}]}]}}

{“access_token”:”N2L7KXa084WvelONYjkJ_traBMCCvy_UKmpUUzlrQ0EA2yNp3Iz6eSUrRG0bhaR_viswd50vDuPkY5nG43d1gbm-olT2KRMxOsVE08RfeD9lvK9lMguNG9kpIkKGZEjIf8Jv2m9fFhf8bnNa-yQH3g”,”expires_in”:7200}
参数表达如下

美食做法删除 接口表明

 

撤回当前应用的自定蒿子杆单。

参数表明如下

恳请表明

图片 5

http诉求方式:GET

回到表达

其中的

对应成立接口,准确的Json重回结果:

 代码如下

 代码如下

N2L7KXa084WvelONYjkJ_traBMCCvy_UKmpUUzlrQ0EA2yNp3Iz6eSUrRG0bhaR_viswd50vDuPkY5nG43d1gbm-olT2KRMxOsVE08RfeD9lvK9lMguNG9kpIkKGZEjIf8Jv2m9fFhf8bnNa-yQH3g

{“errcode”:0,”errmsg”:”ok”}

就是access token。

 

或然应用官方的接口调节和测验工具,地址为:

接受网页调试工具调节和测量检验自定蓬花菜单接口

图片 6

点击检查难题得,获得
图片 7

与此相类似也获取了access token

五、协会菜单内容

时下自定菊花菜单最多饱含3个拔尖菜单,各种一流菜单最多带有5个二级菜单。一流菜单最多4个汉字,二级菜单最多7个汉字,多出去的一些将会以“…”代替。请小心,创造自定菊花菜单后,由于Wechat顾客端缓存,须求24钟头Wechat顾客端才展销会现出来。提出测验时能够尝试撤废关心群众账号后再也关心,则可以观察创立后的效益。

最近自定鹅菜单接口可完成二种档案的次序开关,如下:

click:
顾客点击click类型按键后,Wechat服务器会经过音讯接口推送新闻类型为event
的协会给开辟者(新民日报接口指南),而且带上开关中开辟者填写的key值,开拓者能够通过自定义的key值与客户举办互相;
view:
客户点击view类型开关后,Wechat客商端将会展开开荒者在按键中填入的url值
(即网页链接),到达展开网页的目标,提出与网页授权获取客户大旨音信接口结合,取得客商的登伍人新闻。

接口调用央求表达

http诉求情势:POST(请使用https合同)

须要示例

 {
     “button”:[
     {   
          “type”:”click”,
          “name”:”今日歌曲”,
          “key”:”V1001_TODAY_MUSIC”
      },
      {
           “type”:”click”,
           “name”:”歌星简单介绍”,
           “key”:”V1001_TODAY_SINGER”
      },
      {
           “name”:”菜单”,
           “sub_button”:[
           {   
               “type”:”view”,
               “name”:”搜索”,
               “url”:””
            },
            {
               “type”:”view”,
               “name”:”视频”,
               “url”:””
            },
            {
               “type”:”click”,
               “name”:”赞一下大家”,
               “key”:”V1001_GOOD”
            }]
       }]
 }
参数表达

重回结果

科学时的回到JSON数据包如下:

{“errcode”:0,”errmsg”:”ok”}
张冠李戴时的回到JSON数据包如下(示例为无用菜单名长度):

{“errcode”:40018,”errmsg”:”invalid button name size”}

六、提交菜单内容给服务器

菜单的JSON结构为

1
{“button”:[{“name”:”天气预报”,”sub_button”:[{“type”:”click”,”name”:”北京气象”,”key”:”天气东京(Tokyo卡塔尔国”},{“type”:”click”,”name”:”Hong Kong天气”,”key”:”天气香港”},{“type”:”click”,”name”:”新北天气”,”key”:”气候苏黎世”},{“type”:”click”,”name”:”布拉迪斯拉发天气”,”key”:”气候深圳”},{“type”:”view”,”name”:”当地天气”,”url”:”
将以下代码保存为menu.php,况兼在浏览器中运作该公文(比如

< ?php
 
$access_token = “”;
 
$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”:””
            }]
 
 
       },
       {
           “name”:”方倍职业室”,
           “sub_button”:[
            {
               “type”:”click”,
               “name”:”集团简单介绍”,
               “key”:”company”
            },
            {
               “type”:”click”,
               “name”:”乐趣游戏”,
               “key”:”游戏”
            },
            {
                “type”:”click”,
                “name”:”讲个笑话”,
                “key”:”笑话”
            }]
 
 
       }]
 }’;
 
 
$url =
“;
$result = https_request($url,
$jsonmenu);
var_dump($result);
 
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;
}
 
?>
依然使用官方的调治将养接口 使用网页调试工具调节和测量试验该接口

 

图片 8

图片 9

提交成功后,重新关切后就能够看出菜单。

刚烈推荐使用官方的网页调节和测验工具调节和测量检验该接口。

发表评论

电子邮件地址不会被公开。 必填项已用*标注