携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第27天,点击查看活动详情
设置微信公众号的菜单。
其实就是传一个json给微信的服务器就可以了。比较简单。但是一定要注意json的格式一定要合规,否则会设置失败。
具体步骤:
1:登录微信公众号,点击开发者工具菜单
2:点击在线接口调试工具
3:选择接口类型:如下图,我选择的是自定义菜单
填写access_token值,关于如何获取accesstoken值,请参见《微信公众号开发(二)微信公众号的access_token》
最后,将想要设置菜单的json写入body中。
点击检查问题
4:我这里放一个body中填写的json的实例(其中包含了打开菜单跳转小程序的案例):
{
"button": [
{
"name": "资源下载",
"sub_button": [
{
"type": "view",
"name": "小游戏",
"url": "https://www.xxxxx.net/index.html",
"sub_button": []
},
{
"type": "view",
"name": "关于我们",
"url": "http://www.xxxxx.net/",
"sub_button": []
},
{
"type": "view",
"name": "宣传资料",
"url": "https://www.xxxxx.net/xuanchuanzhiyuan.html",
"sub_button": []
},
{
"type": "view",
"name": "食品检测下载",
"url": "https://www.xxxxx.net/fa0.html",
"sub_button": []
},
{
"type": "view",
"name": "Mlabs下载",
"url": "https://www.xxxxx.net/chemlab.html",
"sub_button": []
}
]
},
{
"name": "教学资源",
"sub_button": [
{
"type": "view",
"name": "开发者",
"url": "https://www.xxxxx.net/LoginDev",
"sub_button": []
},
{
"type": "view",
"name": "在线测试",
"url": "https://www.xxxxx.net/enterExam/1460",
"sub_button": []
},
{
"type": "view",
"name": "虚拟实验",
"url": "https://www.xxxxx.net/toLabExp/",
"sub_button": []
},
{
"type": "view",
"name": "虚拟项目",
"url": "https://www.xxxxx.net/VirtualProject.html",
"sub_button": []
},
{
"type": "view",
"name": "开放课程",
"url": "https://www.xxxxx.net/index.php/openLogin/",
"sub_button": []
}
]
},
{
"name": "系统平台",
"sub_button": [
{
"type": "view",
"name": "中心管理",
"url": "https://www.xxxxx.net/loginCenterSys",
"sub_button": []
},
{
"type": "miniprogram",
"name": "moolsnet小程序",
"url": "https://www.xxxxx.net/",
"sub_button": [],
"appid": "你的小程序appid",
"pagepath": "pages/index/index"
},
{
"type": "miniprogram",
"name": "学生系统",
"url": "https://www.xxxxxx.net/",
"sub_button": [],
"appid": "你的小程序appid",
"pagepath": "pages/index/index"
},
{
"type": "view",
"name": "教师系统",
"url": "https://www.mools.net/",
"sub_button": []
}
]
}
]
}
大概就是这样,这个json一定要符合微信的格式,不然是会报错的。
5:具体实现代码:
/**
* @name: 获取微信access_token
* @desc: 描述
* @author: camellia
* @date: 20211127
* @param: data type description
* @return: data type description
*/
public function getAccessToken()
{
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . WX_APPID . "&secret=" . WX_SECRET;
$info = file_get_contents($url);
// 解析完成,这是一个对象
$obj = json_decode($info);
$access_token = $obj->access_token;
return $access_token;
}
/**
* @name: 设置微信自定义菜单
* @desc: 描述
* @author: camellia
* @date: 20211127
* @param: data type description
* @return: data type description
*/
public function setCaiDan()
{
$data = '上方的json,这里不重复写了。';
// 设置微信自定义菜单
$url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$this->access_token();
$output = $this->curlPost($url, $data);
echo $output;
}
/**
* @name curl_post公共请求方法
* @author camellia
* @date 20220630
* @email: guanchao_gc@qq.com
* @param $url 请求连接
* @param $data 请求参数
*/
public function curlPost($url, $data)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
// POST数据
curl_setopt($ch, CURLOPT_POST, 1);
// 把post的变量加上
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
上边的代码直接可以使用(将你的appid及appscert更换一下)。
有好的建议,请在下方输入你的评论。
欢迎访问个人博客 guanchao.site
欢迎访问我的小程序:打开微信->发现->小程序->搜索“时间里的”