转自http://deng7784033.blog.163.com/blog/static/3826426620138510731934/
如既往的,关于微信公众平台的开发,不算出自大牛的代码,但是简单易懂适合刚入门的朋友。
<?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);//创建菜单
?>