微信公众号开发教程[011]-自定义菜单以及个性化菜单

      自定义菜单最多包括3个一级菜单,每个一级菜单最多包含5个二级菜单.类型分别有click,view,scancode_push,scancode_waitmsg,pic_sysphoto,pic_photo_or_album,pic_weixin,location_select,media_id,view_limited.详见官方文档.

 

 

一.通用的自定义菜单

1).创建通用菜单.代码如下:

 

function create() {
	$ACCESS_TOKEN = "uZNdRwxNN3V7RwT5x_K8K21wNWfO1kX4dMH3JS0usWmXXMIB03-WWoTfMg8as1NyuznLfZK_GqYgJgqiYwIRO4dCK0ZbYNQd-5cxYSX5awA3TkEE-5Qn4QTneX8YIpnTJWJfABAZMZ";
	$url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=$ACCESS_TOKEN";
	$data = '
{
	"button": [
	{
		"type": "click",
		"name": "今日歌曲",
		"key": "V1001_TODAY_MUSIC"
	},
	{
		"name": "菜单",
		"sub_button": [
		{
			"type": "click",
			"name": "1.click.赞一下我们",
			"key": "V1001_GOOD"
		},
		{
			"type": "view",
			"name": "2.view.视频",
			"url": "http://v.qq.com/"
		},
		{
			"type": "scancode_push",
			"name": "3.scancode_push.扫码推事件",
			"key": "rselfmenu_0_1",
			"sub_button": []
		},
		{
			"type": "scancode_waitmsg",
			"name": "4.scancode_waitmsg.扫码带提示",
			"key": "rselfmenu_0_0",
			"sub_button": []
		},
		{
			"type": "pic_sysphoto",
			"name": "5.pic_sysphoto.系统拍照发图",
			"key": "rselfmenu_1_0",
			"sub_button": []
		}]
	},
	{
		"name": "菜单",
		"sub_button": [
		{
			"type": "pic_photo_or_album",
			"name": "6.pic_photo_or_album.拍照/相册发图",
			"key": "rselfmenu_1_1",
			"sub_button": []
		},
		{
			"type": "pic_weixin",
			"name": "7.pic_weixin.微信相册发图",
			"key": "rselfmenu_1_2",
			"sub_button": []
		},
		{
			"type": "location_select",
			"name": "8.location_select.发送位置",
			"key": "rselfmenu_2_0"
		}]
	}]
}
';
	$result = curl_http_post($url, $data);
	echo $result;
}

 

返回结果如下:

 

{"errcode":0,"errmsg":"ok"}

 

 

 

2).查询通用菜单.代码如下:

 

function query() {
	$ACCESS_TOKEN = "uZNdRwxNN3V7RwT5x_K8K21wNWfO1kX4dMH3JS0usWmXXMIB03-WWoTfMg8as1NyuznLfZK_GqYgJgqiYwIRO4dCK0ZbYNQd-5cxYSX5awA3TkEE-5Qn4QTneX8YIpnTJWJfABAZMZ";
	$url = "https://api.weixin.qq.com/cgi-bin/menu/get?access_token=$ACCESS_TOKEN";
	$result = curl_http_get($url);
	echo $result;
}

 

返回结果如下:

 

{
	"button": [
	{
		"type": "click",
		"name": "今日歌曲",
		"key": "V1001_TODAY_MUSIC"
	},
	{
		"name": "菜单",
		"sub_button": [
		{
			"type": "click",
			"name": "1.click.赞一下我们",
			"key": "V1001_GOOD"
		},
		{
			"type": "view",
			"name": "2.view.视频",
			"url": "http://v.qq.com/"
		},
		{
			"type": "scancode_push",
			"name": "3.scancode_push.扫码推事件",
			"key": "rselfmenu_0_1",
			"sub_button": []
		},
		{
			"type": "scancode_waitmsg",
			"name": "4.scancode_waitmsg.扫码带提示",
			"key": "rselfmenu_0_0",
			"sub_button": []
		},
		{
			"type": "pic_sysphoto",
			"name": "5.pic_sysphoto.系统拍照发图",
			"key": "rselfmenu_1_0",
			"sub_button": []
		}]
	},
	{
		"name": "菜单",
		"sub_button": [
		{
			"type": "pic_photo_or_album",
			"name": "6.pic_photo_or_album.拍照/相册发图",
			"key": "rselfmenu_1_1",
			"sub_button": []
		},
		{
			"type": "pic_weixin",
			"name": "7.pic_weixin.微信相册发图",
			"key": "rselfmenu_1_2",
			"sub_button": []
		},
		{
			"type": "location_select",
			"name": "8.location_select.发送位置",
			"key": "rselfmenu_2_0"
		}]
	}]
}

 

 

 

3).获取自定义菜单配置接口.代码如下:

function info() {
	$ACCESS_TOKEN = "uZNdRwxNN3V7RwT5x_K8K21wNWfO1kX4dMH3JS0usWmXXMIB03-WWoTfMg8as1NyuznLfZK_GqYgJgqiYwIRO4dCK0ZbYNQd-5cxYSX5awA3TkEE-5Qn4QTneX8YIpnTJWJfABAZMZ";
	$url = "https://api.weixin.qq.com/cgi-bin/get_current_selfmenu_info?access_token=$ACCESS_TOKEN";
	$result = curl_http_get($url);
	echo $result;
}

 

 

我这里和"2).查询通用菜单"结果一样.返回结果如下:

{
	"button": [
	{
		"type": "click",
		"name": "今日歌曲",
		"key": "V1001_TODAY_MUSIC"
	},
	{
		"name": "菜单",
		"sub_button": [
		{
			"type": "click",
			"name": "1.click.赞一下我们",
			"key": "V1001_GOOD"
		},
		{
			"type": "view",
			"name": "2.view.视频",
			"url": "http://v.qq.com/"
		},
		{
			"type": "scancode_push",
			"name": "3.scancode_push.扫码推事件",
			"key": "rselfmenu_0_1",
			"sub_button": []
		},
		{
			"type": "scancode_waitmsg",
			"name": "4.scancode_waitmsg.扫码带提示",
			"key": "rselfmenu_0_0",
			"sub_button": []
		},
		{
			"type": "pic_sysphoto",
			"name": "5.pic_sysphoto.系统拍照发图",
			"key": "rselfmenu_1_0",
			"sub_button": []
		}]
	},
	{
		"name": "菜单",
		"sub_button": [
		{
			"type": "pic_photo_or_album",
			"name": "6.pic_photo_or_album.拍照/相册发图",
			"key": "rselfmenu_1_1",
			"sub_button": []
		},
		{
			"type": "pic_weixin",
			"name": "7.pic_weixin.微信相册发图",
			"key": "rselfmenu_1_2",
			"sub_button": []
		},
		{
			"type": "location_select",
			"name": "8.location_select.发送位置",
			"key": "rselfmenu_2_0"
		}]
	}]
}

 

 

3).删除通用菜单.代码如下:

 

 

function delete() {
	$ACCESS_TOKEN = "uZNdRwxNN3V7RwT5x_K8K21wNWfO1kX4dMH3JS0usWmXXMIB03-WWoTfMg8as1NyuznLfZK_GqYgJgqiYwIRO4dCK0ZbYNQd-5cxYSX5awA3TkEE-5Qn4QTneX8YIpnTJWJfABAZMZ";
	$url = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=$ACCESS_TOKEN";
	$result = curl_http_get($url);
	echo $result;
}

 

返回结果如下:

{"errcode":0,"errmsg":"ok"}

 

 

4).自定义菜单事件推送.

 

 

        用户点击自定义菜单后,微信会把点击事件推送给开发者.我的结果如下:

 


	
	
	1468506310
	
	
	


	
	
	1468506313
	
	
	
	413699252


	
	
	1468506514
	
	
	
	
		
		
	


	
	
	1468506624
	
	
	
	
		
		
	


	
	
	1468506760
	
	
	
	
		1
		
			
				
			
		
	


	
	
	1468506942
	
	
	
	
		1
		
			
				
			
		
	


	
	
	1468507103
	
	
	
	
		0
		
	


	
	
	1468507124
	
	
	
	
		
		
		
		
		
		
	

 

 

 

二.个性化菜单

1 创建个性化菜单.代码如下:

 

function add() { //创建个性化菜单
	$ACCESS_TOKEN = "uZNdRwxNN3V7RwT5x_K8K21wNWfO1kX4dMH3JS0usWmXXMIB03-WWoTfMg8as1NyuznLfZK_GqYgJgqiYwIRO4dCK0ZbYNQd-5cxYSX5awA3TkEE-5Qn4QTneX8YIpnTJWJfABAZMZ";
	$url = "https://api.weixin.qq.com/cgi-bin/menu/addconditional?access_token=$ACCESS_TOKEN";
	$data = '
{
	"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"
		}]
	}],
	"matchrule":
	{
		"sex": "1",
		"country": "中国",
		"province": "广东",
		"city": "深圳",
		"client_platform_type": "1",
		"language": "zh_CN"
	}
}
';
	$result = curl_http_post($url, $data);
	echo $result;
}

 

返回结果如下:

{"errcode":0,"errmsg":"ok"}

 

3).测试个性化菜单匹配结果.代码如下:

 

 

function match() { //测试个性化菜单匹配结果
	$ACCESS_TOKEN = "uZNdRwxNN3V7RwT5x_K8K21wNWfO1kX4dMH3JS0usWmXXMIB03-WWoTfMg8as1NyuznLfZK_GqYgJgqiYwIRO4dCK0ZbYNQd-5cxYSX5awA3TkEE-5Qn4QTneX8YIpnTJWJfABAZMZ";
	$url = "https://api.weixin.qq.com/cgi-bin/menu/trymatch?access_token=$ACCESS_TOKEN";
	$data = '{"user_id":"szuzsq"}';
	$result = curl_http_post($url, $data);
	echo $result;
}

 

返回结果如下:

 

{
	"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"
		}]
	}]
}

 

4). 删除个性化菜单.代码如下:

 

function del() { //删除个性化菜单
	$ACCESS_TOKEN = "uZNdRwxNN3V7RwT5x_K8K21wNWfO1kX4dMH3JS0usWmXXMIB03-WWoTfMg8as1NyuznLfZK_GqYgJgqiYwIRO4dCK0ZbYNQd-5cxYSX5awA3TkEE-5Qn4QTneX8YIpnTJWJfABAZMZ";
	$url = "https://api.weixin.qq.com/cgi-bin/menu/delconditional?access_token=$ACCESS_TOKEN";
	$data = '{"menuid":"413705287"}';
	$result = curl_http_post($url, $data);
	echo $result;
}

 

返回结果如下:

{"errcode":0,"errmsg":"ok"}

 

5).查询个性化菜单.

 

使用普通自定义菜单查询接口可以获取默认菜单和全部个性化菜单信息.

6).删除个性化菜单

使用普通自定义菜单删除接口可以删除所有自定义菜单(包括默认菜单和全部个性化菜单).

 

三.效果图

        我只看到了通用的自定义菜单,个性化菜单没看到.官方档上说:"个性化菜单要求用户的微信客户端版本在iPhone6.2.2,Android6.2.4以上".4s的吊丝表示伤不起.如下图:

微信公众号开发教程[011]-自定义菜单以及个性化菜单_第1张图片微信公众号开发教程[011]-自定义菜单以及个性化菜单_第2张图片

 

 

 

 

你可能感兴趣的:(微信公众号开发教程[011]-自定义菜单以及个性化菜单)