Home >Backend Development >PHP Tutorial >WeChat public platform custom menu development example_PHP tutorial

WeChat public platform custom menu development example_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:43:541168browse

The WeChat public platform has launched a custom menu function. We can simply edit the custom menu directly in the background, or use the API to set the menu. Let me introduce the operation method below.

Tencent WeChat officially launched on March 19 Announce the custom menu function of the public platform. Currently, this function is only available to enterprises and institutions, and internal testing qualifications require application. For this new feature, only a few accounts have been activated. At this point, the commercial value of WeChat has been further explored.

WeChat public platform custom menu development example_PHP tutorial

Developers can use this feature to add a custom menu to the bottom of the conversation interface of public accounts. Users can click on the options in the menu to call up the corresponding reply information or web link.

Menu Creation
Interface Description
Create a custom menu on the WeChat client by POSTing a specific structure.
Request instructions
http request method: POST

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

The structure is actually a string in json format. I believe many friends are stuck here. How to post a structure to this interface address? The implementation code is as follows:

The code is as follows Copy code

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":"Hotel reservation","+
""sub_button":["+
"{"+
""type":"click","+
""name":"Nearby Hotel","+
""key":"jintoneinn488878-1""+
"},"+
"{"+
""type":"click","+
""name":"Find hotel","+
""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());
}


Menu query
Interface Description

Query the currently used custom menu structure.

Request instructions

http request method: GET

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

Corresponds to the creation interface, and the correct Json return result is:

Return instructions
The code is as follows
 代码如下 复制代码

{"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 code


{"menu":{"button":[{"type":"click","name":"Today's song","key":"V1001_TODAY_MUSIC","sub_button":[]},{" type":"click","name":"Singer Profile","key":"V1001_TODAY_SINGER","sub_button":[]},{"name":"Menu","sub_button":[{"type" :"click","name":"hello word","key":"V1001_HELLO_WORLD","sub_button":[]},{"type":"click","name":"Like us"," key":"V1001_GOOD","sub_button":[]}]}]}}

Menu Delete


Interface Description

Cancel the currently used custom menu.

Request instructions
 代码如下 复制代码
{"errcode":0,"errmsg":"ok"}

http request method: GET

https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=ACCESS_TOKEN
Corresponds to the creation interface, and the correct Json return result is:
The code is as follows

Copy code
{"errcode":0,"errmsg" :"ok"} http://www.bkjia.com/PHPjc/633140.htmlwww.bkjia.com
truehttp: //www.bkjia.com/PHPjc/633140.htmlTechArticleWeChat public platform has launched a custom menu function. We can simply edit the custom menu directly in the background, or we can Use the API to set up the menu. Let me introduce the operation method...
Statement:
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