search
HomeWeChat AppletWeChat DevelopmentShare sample code for developing custom menus on WeChat

Before WeChat 5.0, custom menu was used as an internal test qualification. Only a few public accounts had menus, so many companies spent a lot of money to buy menus. Nowadays, a large number of accounts have been converted from subscription accounts to service accounts, and many of them are rushing to customize the menu. Today we will briefly study the processing of WeChat custom menu.

Creation of custom menu

<?php
 
define("APPID", "您的appid");
define("APPSECRET", "您的appsecret ");
 
$token_access_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . APPID . "&secret=" . APPSECRET;
$res = file_get_contents($token_access_url);  //获取文件内容或获取网络请求的内容
//echo $res;
$result = json_decode($res, true);  //接受一个 JSON 格式的字符串并且把它转换为 PHP 变量
$access_token = $result[&#39;access_token&#39;];
 
define("ACCESS_TOKEN", $access_token);  //将access_token定义为常量,便于使用.
 
$make_menu_url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" . ACCESS_TOKEN;
 
$menuData = &#39; {
   "button":[
   {
     "type":"click",
     "name":"今日歌曲",
     "key":"V1001_TODAY_MUSIC"
   },
   {
      "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"
      }]
    }]
 }&#39;;
 
$ch = curl_init();
 
curl_setopt($ch, CURLOPT_URL, $make_menu_url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $menuData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
 
$info = curl_exec($ch);
 
//判读执行过程中是否有错误,有则发送数据错误报告.
if (curl_errno($ch)) {
  echo &#39;Error&#39; . curl_error($ch); //用户检查php运行环境中的curl模块开启情况.
}
 
curl_close($ch);
print_r($info); //查看post提交到微信服务器后,返回的数据.

Getting custom menu

<?php
 
define("APPID", "您的appid");
define("APPSECRET", "您的appsecret ");
 
$token_access_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . APPID . "&secret=" . APPSECRET;
$res = file_get_contents($token_access_url);  //获取文件内容或获取网络请求的内容
$result = json_decode($res, true);  //接受一个 JSON 格式的字符串并且把它转换为 PHP 变量
$access_token = $result[&#39;access_token&#39;];
 
$make_menu_url = "https://api.weixin.qq.com/cgi-bin/menu/get?access_token=" . $access_token;
 
$menu_json = file_get_contents($make_menu_url);
 
echo $menu_json;

##Delete of custom menu

<?php
 
define("APPID", "您的appid");
define("APPSECRET", "您的appsecret ");
 
$token_access_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . APPID . "&secret=" . APPSECRET;
$res = file_get_contents($token_access_url);  //获取文件内容或获取网络请求的内容
$result = json_decode($res, true);  //接受一个 JSON 格式的字符串并且把它转换为 PHP 变量
$access_token = $result[&#39;access_token&#39;];
 
$make_menu_url = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=" . $access_token;
 
$menu_json = file_get_contents($make_menu_url);
 
echo $menu_json;

The above is the entire content of this article. I hope it will be helpful to everyone in WeChat development.

The above is the detailed content of Share sample code for developing custom menus on WeChat. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.