Code
<?php
$appid = "xxxxxx";
$appsecret = "xxxxxx";
$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"];
$jsonmenu = '{
"button": [
{
"name": "扫码",
"sub_button": [
{
"type": "scancode_waitmsg",
"name": "扫码带提示",
"key": "rselfmenu_0_0"
},
{
"type": "scancode_push",
"name": "扫码推事件",
"key": "rselfmenu_0_1"
}
]
},
{
"name": "发图",
"sub_button": [
{
"type": "pic_sysphoto",
"name": "系统拍照发图",
"key": "rselfmenu_1_0"
},
{
"type": "pic_photo_or_album",
"name": "拍照或者相册发图",
"key": "rselfmenu_1_1"
},
{
"type": "pic_weixin",
"name": "微信相册发图",
"key": "rselfmenu_1_2"
}
]
}
]
}';
$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会话
// 用curl_setopt()设置传输选项
curl_setopt($curl, CURLOPT_URL, $url); // 需要获取的URL地址
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); // 禁止验证HTTPS证书是否有效
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); // 禁止检查SSL证书
if (!empty($data)){
curl_setopt($curl, CURLOPT_POST, 1); // 发送POST请求
curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // 传参
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 将curl_exec()获取的信息以字符串返回,而不是直接输出
$output = curl_exec($curl); // 执行cURL会话
curl_close($curl); // 关闭cURL会话
return $output; // 返回结果
}
?>
参数说明
注:创建自定义菜单后,菜单的刷新策略是,在用户进入公众号会话页或公众号profile页时,如果发现上一次拉取菜单的请求在5分钟以前,就会拉取一下菜单,如果菜单有更新,就会刷新客户端的菜单。测试时可以尝试取消关注公众账号后再次关注,则可以看到创建后的效果。