Home > php教程 > php手册 > 微信公众平台自定义菜单开发示例

微信公众平台自定义菜单开发示例

WBOY
Release: 2016-06-02 09:14:42
Original
1748 people have browsed it

微信公众平台推出一自定义菜单功能,我们可以简单的直接在后台编辑自定义菜单,也可以利用api来设置菜单,下面我来介绍一下操作方法

 腾讯微信3月19日正式公布公众平台自定义菜单功能。目前该功能仅提供给企业和机构,且内测资格需要申请。对于这一新增功能,目前仅有少数账号已经开通。至此,微信的商业化价值被进一步挖掘出来。

 

微信公众平台推出自定义菜单功能

 

   

开发者可利用该功能为公众账号的会话界面底部增加自定义菜单。用户点击菜单中的选项,可以调出相应的回复信息或网页链接。

菜单创建

接口说明

通过POST一个特定结构体,实现在微信客户端创建自定义菜单。

请求说明

http请求方式:POST

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

结构体其实就是一个json格式的字符串,相信很多朋友就卡在这里了,怎么往这个接口地址 post 一个结构体呢,实现代码如下:

public static void main(String[] args) throws Exception { 
URL url = new URL("https://api.weixin.qq.com/cgi-bin 
                             /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());  
}
Copy after login

菜单查询

接口说明

查询当前使用的自定义菜单结构。

请求说明

http请求方式:GET

https://api.weixin.qq.com/cgi-bin/menu/get?access_token=ACCESS_TOKEN
Copy after login

返回说明

对应创建接口,正确的Json返回结果:

{"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":[]}]}]}}
Copy after login

菜单删除

接口说明

取消当前使用的自定义菜单。

请求说明

http请求方式:GET

https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=ACCESS_TOKEN
Copy after login

返回说明

对应创建接口,正确的Json返回结果:

{"errcode":0,"errmsg":"ok"}
Copy after login

 

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template