开发微信菜单栏除了使用微信编辑自定义菜单功能外,我们开发者还可以通过编写代码来自定义菜单。
首先在本地(或服务器下)编辑以下代码并运行(或者curl模拟请求)
说明
文件名为make_menu.php
1、点击我要预订
,跳转到…页面
2、点击最新活动
,显示二级菜单栏
3、点击神秘房客报名
,请求配置文件,返回相应事件。
<?php
//微信公众平台appid和appsecret
$appid = "你的appid";
$appsecret = "你的appsecret";
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret";
$output = https_request($url);
$jsoninfo = json_decode($output, true);
$access_token = $jsoninfo["access_token"];
//print_r($access_token);die;
$jsonmenu = ‘{
"button":[
{
"type":"view",
"name":"我要预订",
"url":"http://www.test.com/yuding"
},
{
"name":"最新活动",
"sub_button":[
{
"type":"view",
"name":"免押金住宿",
"url":"http://www.test.com/mianyajin"
},
{
"type":"view",
"name":"房东招募",
"url":"http://www.test.com/zhaomu"
},
{
"type":"view",
"name":"恋家优选",
"url":"http://www.test.com/youxuan"
},
{
"type":"click",
"name":"神秘房客报名",
"key":"mtr"
}]
},
{
"name":"更多服务",
"sub_button":[
{
"type":"view",
"name":"下载APP",
"url":"http://www.test.com/download"
},
{
"type":"view",
"name":"房东助手",
"url":"http://www.test.com/zhushou"
},
{
"type":"view",
"name":"查询订单",
"url":"http://www.test.com/dingdan"
},
{
"type":"click",
"name":"房东指南",
"key":"lg"
},
{
"type":"click",
"name":"房客指南",
"key":"tg"
}]
},
]
}‘;
$url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$access_token;
$result = https_request($url, $jsonmenu);
var_dump($result);
function https_request($url,$data = null){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
if (!empty($data)){
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);
curl_close($curl);
return $output;
}
?>