建立自定义菜单,实现菜单事件。php
接口:html
https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
我用的是测试号,修改APPID和APPSECRET,而后浏览器访问上面这个Url便可生成Access_Tokenapi
<?php header("Content-type: text/html; charset=utf-8"); define("ACCESS_TOKEN", "生成的Access_Token"); //建立菜单 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; } //获取菜单 function getMenu(){ return file_get_contents("https://api.weixin.qq.com/cgi-bin/menu/get?access_token=".ACCESS_TOKEN); } //删除菜单 function deleteMenu(){ return file_get_contents("https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=".ACCESS_TOKEN); } $data = '{ "button":[ { "type":"click", "name":"首页", "key":"home" }, { "type":"click", "name":"简介", "key":"introduct" }, { "name":"菜单", "sub_button":[ { "type":"click", "name":"hello word", "key":"V1001_HELLO_WORLD" }, { "type":"click", "name":"赞一下咱们", "key":"V1001_GOOD" }] }] }'; echo createMenu($data);
正确时的返回JSON数据包以下:浏览器
{"errcode":0,"errmsg":"ok"}
错误时的返回JSON数据包以下(示例为无效菜单名长度):微信
{"errcode":40018,"errmsg":"invalid button name size"}
做者:TANKING
微信:face6009
网站:https://www.likeyunba.comapp