一如既往的,关于微信公众平台的开发,不算出自大牛的代码,但是简单易懂适合刚入门的朋友。
<?php
header("Content-type: text/html; charset=utf-8");
define("ACCESS_TOKEN", 'NYD-0s6f4yBJecqJ6QAiH7Nivh_gnWhbjs5vY1BZ9zViZAkYfw0QL5cO5ILK7lS3BkUCUrwqlDqzU9OVMaDO44Vuju7ZhXjdpJk8PxHgNf7cTs7rp6dvm-KJSkdiufqbL5niZwHM0uWJVi0mnSLGZg');
//创建菜单
function createMenu($data){
$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);
$tmpInfo = curl_exec($ch);
if (curl_errno($ch)) {
return curl_error($ch);
}
curl_close($ch);
return $tmpInfo;
}
$data = ' {
"button":[
{
"name":"时讯",
"sub_button":[
{
"type":"click",
"name":"即时新闻",
"key":"jsxw"
},
{
"type":"click",
"name":"财经早餐",
"key":"cjzc"
},
{
"type":"click",
"name":"焦点话题",
"key":"jdht"
},
{
"type":"view",
"name":"财经日历",
"url":"http://stock1.sina.cn/dpool/stockv2/universal_calendar.php?vt=4"
}]
},
{
"name":"说市",
"sub_button":[
{
"type":"click",
"name":"今日看盘",
"key":"jrkp"
},
{
"type":"view",
"name":"行情刷新",
"url":"http://XX/index.php/market/index"
},
{
"type":"click",
"name":"交易策略",
"key":"jycl"
},
{
"type":"click",
"name":"投资天地",
"key":"tztd"
},
{
"type":"click",
"name":"投资文库",
"key":"tzwk"
}]
},
{
"name":"北鼎在线",
"sub_button":[
{
"type":"view",
"name":"关于我们",
"url":"http://www.index.php/article/index/id/114"
},
{
"type":"view",
"name":"最新活动",
"url":"http://www.index.php/article/index/id/115"
},
{
"type":"view",
"name":"参与爆料",
"url":"http://www.index.php/message"
}
]
}
]
}';
echo createMenu($data);//创建菜单
?>
如代码所示,菜单有两种,一种是直接跳转到URL页面,即view;另一种,是发送指令,即click,说白了就是在客户端辅助用户的,可以看成一键发关键词。第一种很好做,填写好URL就完事了,用户点击菜单按钮,然后跳转到你填写的URL地址,缺点是URL地址不变,不够灵活。另一种就是发送指令,可以在公众账号接口开发(注意跟自定义菜单区别开)的时候,判断指令的内容,灵活处理并且动态输出不同的数据。