接口地址
http请求方式:POST(请使用https协议) https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN
参数说明
参数 | 是否必须 | 说明 |
---|---|---|
button | 是 | 一级菜单数组,个数应为1~3个 |
sub_button | 否 | 二级菜单数组,个数应为1~5个 |
type | 是 | 菜单的响应动作类型,view表示网页类型,click表示点击类型,miniprogram表示小程序类型 |
name | 是 | 菜单标题,不超过16个字节,子菜单不超过60个字节 |
key | click等点击类型必须 | 菜单KEY值,用于消息接口推送,不超过128字节 |
url | view、miniprogram类型必须 | 网页 链接,用户点击菜单可打开链接,不超过1024字节。 type为miniprogram时,不支持小程序的老版本客户端将打开本url。 |
media_id | media_id类型和view_limited类型必须 | 调用新增永久素材接口返回的合法media_id |
appid | miniprogram类型必须 | 小程序的appid(仅认证公众号可配置) |
pagepath | miniprogram类型必须 | 小程序的页面路径 |
返回结果
{"errcode":0,"errmsg":"ok"}
PHP代码示例
<?php
class wxMenu {
public function createMenu () {
$ACCESS_TOKEN = "";//通过微信获取access_token接口 获取的token
$data = '{
"button":[
{
"name":"产品中心",
"sub_button":[
{
"type":"view",
"name":"标准产品",
"url":"http://www.szvdt.com/nav/9.html"
},
{
"type":"view",
"name":"定制产品",
"url":"http://www.szvdt.com/nav/9.html"
},
{
"type":"view",
"name":"经典案例",
"url":"http://www.szvdt.com/nav/12.html"
}]
},
{
"name":"服务",
"sub_button":[
{
"type": "view_limited",
"name": "公司简介",
"media_id": ""
},
{
"type": "view_limited",
"name": "公司动态",
"media_id": ""
},
{
"type": "view_limited",
"name": "技术支持",
"media_id": ""
}]
},
{
"name":"更多",
"sub_button":[
{
"type":"view",
"name":"企业官网",
"url":"http://www.szvdt.com/"
},
{
"type":"view",
"name":"阿里巴巴",
"url":"http://szvdt01.1688.com"
},
{
"type": "view_limited",
"name": "联系我们",
"media_id": ""
},
{
"type":"view",
"name":"家校互通",
"url":"http://www.autovdt.com/index.php"
}]
}]
}';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,
"https://api.weixin.qq.com/cgi-bin/menu/create?access_token={$ACCESS_TOKEN}");
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 5.01; Windows NT 5.0)');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$res= curl_exec($ch);
if (curl_errno($ch)) {
echo 'Errno' . curl_error($ch);
}
curl_close($ch);
print_r($res);
}
}
$wx_menu = new wxMenu();
$wx_menu->createMenu();