目前微信服务号自定义菜单最多包括3个一级菜单,每个一级菜单最多包含5个二级菜单。一级菜单最多4个汉字,二级菜单最多7个汉字,多出来的部分将会以“...”代替。请注意,创建自定义菜单后,由于微信客户端缓存,需要24小时微信客户端才会展现出来。建议测试时可以尝试取消关注公众账号后再次关注,则可以看到创建后的效果。
目前自定义菜单接口可实现两种类型按钮,如下:
click:
用户点击click类型按钮后,微信服务器会通过消息接口推送消息类型为event 的结构给开发者(参考消息接口指南),并且带上按钮中开发者填写的key值,开发者可以通过自定义的key值与用户进行交互;
view:
用户点击view类型按钮后,微信客户端将会打开开发者在按钮中填写的url值 (即网页链接),达到打开网页的目的,建议与网页授权获取用户基本信息接口结合,获得用户的登入个人信息。
接口调用请求说明
http请求方式:post(请使用https协议) https://api.weixin.qq.com/cgi-bin/menu/create?access_token=access_token
请求示例(json数据请使用utf-8编码)
{
"button":[
{"type":"click","name":"我的信息","sub_button":[
{"type":"click","name":"拇指查询","key":"button_1"},
{"type":"click","name":"拇指请假","key":"button_2"},
{"type":"view","name":"工号绑定","url":"http://xxxxxxxxxxxxxxxxx"}]
},
{"type":"click","name":"业务流程","key":"button_3"},
{"name":"员工建议","sub_button":[
{"type":"view","name":"思想火花","url":"http://xxxxxxxxxxxxxxxxxx"},
{"type":"view","name":"奖品兑换","url":"http://xxxxxxxxxxxxxxxxxx"},
{"type":"click","name":"赞一下我们","key":"button_zan"}]
}
]
}
参数说明
返回结果
正确时的返回json数据包如下:
{"errcode":0,"errmsg":"ok"}
错误时的返回json数据包如下(示例为无效菜单名长度):
{"errcode":40018,"errmsg":"invalid button name size"}
以下是示例代码(php)。
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh-cn">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="author" content="chris mao" />
</head>
<body>
<?php
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=appid&secret=appsecret";
$ch = curl_init($url);
curl_setopt($ch, curlopt_header, 0);
curl_setopt($ch, curlopt_returntransfer, 1);
curl_setopt($ch, curlopt_post, 0);
curl_setopt($ch, curlopt_ssl_verifyhost, false);
curl_setopt($ch, curlopt_ssl_verifypeer, false);
$output = curl_exec($ch);
curl_close($ch);
if (empty($output)) { var_dump($output); exit; }
$result = json_decode($output);
$token = $result->access_token;
//创建菜单
$url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=$token";
$jsondata = file_get_contents("menu.json");
$ch = curl_init($url);
curl_setopt($ch, curlopt_header, 0);
curl_setopt($ch, curlopt_returntransfer, 1);
curl_setopt($ch, curlopt_post, 1);
curl_setopt($ch, curlopt_postfields, $jsondata);
curl_setopt($ch, curlopt_useragent, $_server['http_user_agent']);
curl_setopt($ch, curlopt_ssl_verifyhost, false);
curl_setopt($ch, curlopt_ssl_verifypeer, false);
$output = curl_exec($ch);
curl_close($ch);
var_dump($output);
?>
</body>
</html>
menu.json
{
"button":[
{"type":"click","name":"我的信息","sub_button":[
{"type":"click","name":"拇指查询","key":"button_1"},
{"type":"click","name":"拇指请假","key":"button_2"},
{"type":"view","name":"工号绑定","url":"http://xxxxxxxxxxxxxxxxx"}]
},
{"type":"click","name":"业务流程","key":"button_3"},
{"name":"员工建议","sub_button":[
{"type":"view","name":"思想火花","url":"http://xxxxxxxxxxxxxxxxxx"},
{"type":"view","name":"奖品兑换","url":"http://xxxxxxxxxxxxxxxxxx"},
{"type":"click","name":"赞一下我们","key":"button_zan"}]
}
]
}
响应自定义菜单事件
$wechatobj = new wechatcallbackapi();
if (isset($_get["echostr"])) {
$wechatobj->valid();
} else {
$wechatobj->responsemsg();
}
class wechatcallbackapi {
private $token = "weixin";
private $appid = "appid";
private $appsecret = "appsecret";
private function checksignature() {
$signature = $_get["signature"];
$timestamp = $_get["timestamp"];
$nonce = $_get["nonce"];
$tmparr = array($this->token, $timestamp, $nonce);
sort($tmparr);
$tmpstr = implode($tmparr);
$tmpstr = sha1($tmpstr);
if($tmpstr == $signature) {
return true;
} else {
return false;
}
}
private function getaccesstoken() {
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appid&secret=$this->appsecret";
$ch = curl_init($url);
$curl_setopt($ch, curlopt_header, 0);
$curl_setopt($ch, curlopt_returntransfer, 1);
$curl_setopt($ch, curlopt_post, 0);
$curl_setopt($ch, curlopt_ssl_verifyhost, false);
$curl_setopt($ch, curlopt_ssl_verifypeer, false);
$output = curl_exec($ch);
curl_close($ch);
if (empty($output)) { return ""; }
$result = json_decode($result);
return $result->access_token;
}
public function valid() {
$echostr = $_get["echostr"];
//valid signature, option
if($this->checksignature()){
echo $echostr;
exit;
}
}
public function responsemsg() {
//get post data, may be due to the different environments
$poststr = $globals["http_raw_post_data"];
if (empty($poststr)){
echo "";
exit;
}
//extract post data
$postobj = simplexml_load_string($poststr, 'simplexmlelement', libxml_nocdata);
$fromusername = $postobj->fromusername;
$tousername = $postobj->tousername;
$time = time();
//文本消息模板
$texttpl = "<xml>
<tousername><![cdata[%s]]></tousername>
<fromusername><![cdata[%s]]></fromusername>
<createtime>%s</createtime>
<msgtype><![cdata[%s]]></msgtype>
<content><![cdata[%s]]></content>
<funcflag>0</funcflag>
</xml>";
switch (strtolower(trim($postobj->msgtype))) {
case "text": //文本消息
$keyword = trim($postobj->content);
if(!empty($keyword)) {
$msgtype = "text";
$contentstr = "$fromusername, 您发送了文本信息: $keyword ";
if (strtolower($keyword) == "time") {
$contentstr = date("y-m-d h:i:s", $time);
}
$resultstr = sprintf($texttpl, $fromusername, $tousername, $time, $msgtype, $contentstr);
} else {
$resultstr = "input something...";
}
break;
case "image": //图片消息
$msgtype = "text";
$contentstr = "$fromusername, 您发送了图片信息";
$resultstr = sprintf($texttpl, $fromusername, $tousername, $time, $msgtype, $contentstr);
break;
case "voice": //声音消息
$msgtype = "text";
$contentstr = "$fromusername, 您发送了声音信息";
$resultstr = sprintf($texttpl, $fromusername, $tousername, $time, $msgtype, $contentstr);
break;
case "video": //视频消息
$msgtype = "text";
$contentstr = "$fromusername, 您发送了视频信息";
$resultstr = sprintf($texttpl, $fromusername, $tousername, $time, $msgtype, $contentstr);
break;
case "location": //位置消息
$msgtype = "text";
$contentstr = "$fromusername, 您发送了位置信息";
$resultstr = sprintf($texttpl, $fromusername, $tousername, $time, $msgtype, $contentstr);
break;
case "link": //链接消息
$msgtype = "text";
$contentstr = "$fromusername, 您发送了链接信息";
$resultstr = sprintf($texttpl, $fromusername, $tousername, $time, $msgtype, $contentstr);
break;
case "event": //事件
switch (strtolower(trim($postobj->event))) {
case "subscribe": //关注事件
$msgtype = "text";
$contentstr = "欢迎您关注xxxxxxx";
$resultstr = sprintf($texttpl, $fromusername, $tousername, $time, $msgtype, $contentstr);
break;
case "unsubscribe": //取消关注事件
break;
case "scan": //用户已关注时扫描二维码事件
$msgtype = "text";
$contentstr = "$fromusername, 您扫描了二维码";
$resultstr = sprintf($texttpl, $fromusername, $tousername, $time, $msgtype, $contentstr);
break;
case "location": //上传地理位置事件
$msgtype = "text";
$contentstr = "$fromusername, 您上传地理位置";
$resultstr = sprintf($texttpl, $fromusername, $tousername, $time, $msgtype, $contentstr);
break;
case "click": //自定义菜单事件
$msgtype = "text";
$contentstr = "$fromusername, 您点击了自定义菜单 $postobj->eventkey ";
if ("button_zan" == $postobj->eventkey) {
$contentstr = "感谢您的赞,我们会继续提供更优质的服务。";
}
$resultstr = sprintf($texttpl, $fromusername, $tousername, $time, $msgtype, $contentstr);
;
break;
default:
$resultstr = "";
}
break;
default:
$resultstr = "";
}
echo $resultstr;
}
}
?>
自定义菜单查询
使用接口创建自定义菜单后,开发者还可使用接口查询自定义菜单的结构。
请求说明
http请求方式:get
https://api.weixin.qq.com/cgi-bin/menu/get?access_token=access_token
返回说明
对应创建接口,正确的json返回结果:
自定义菜单删除
使用接口创建自定义菜单后,开发者还可使用接口删除当前使用的自定义菜单。
请求说明
http请求方式:get
https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=access_token
返回说明
对应创建接口,正确的json返回结果:
{"errcode":0,"errmsg":"ok"}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
评论列表:
发布于 3天前回复该评论
发布于 3天前回复该评论
发布于 3天前回复该评论
发布于 3天前回复该评论
发布于 2天前回复该评论
发布于 2天前回复该评论
发布于 2天前回复该评论
发布于 2天前回复该评论