温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

微信开发如何实现自定义菜单

发布时间:2021-07-27 11:14:15 来源:亿速云 阅读:143 作者:小新 栏目:移动开发

这篇文章主要介绍了微信开发如何实现自定义菜单,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

自定义菜单的创建

<?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['access_token'];
 
define("ACCESS_TOKEN", $access_token);  //将access_token定义为常量,便于使用.
 
$make_menu_url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" . ACCESS_TOKEN;
 
$menuData = ' {
   "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"
      }]
    }]
 }';
 
$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 'Error' . curl_error($ch); //用户检查php运行环境中的curl模块开启情况.
}
 
curl_close($ch);
print_r($info); //查看post提交到微信服务器后,返回的数据.

自定义菜单的获取

<?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['access_token'];
 
$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;

自定义菜单的删除

<?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['access_token'];
 
$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;

感谢你能够认真阅读完这篇文章,希望小编分享的“微信开发如何实现自定义菜单”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI