将下面代码写入一个php文件,然后在浏览器中运行即可为指定的公众号创建自定义菜单。
这里创建的都是view类按钮,将其中的‘链接’换成实际地址即可。(创建其他类型的按钮时原理一样)
$access_token = "公众号access_token";
$jsonmenu = '{
"button":[
{
"type":"view",
"name":"主按钮1",
"url":"链接"
},
{
"name":"主按钮2",
"sub_button":[
{
"type":"view",
"name":"子按钮",
"url":"链接"
},
{
"type":"view",
"name":"子按钮",
"url":"链接"
},
{
"type":"view",
"name":"子按钮",
"url":"链接"
}]
},
{
"name":"主按钮3",
"sub_button":[
{
"type":"view",
"name":"子按钮",
"url":"链接"
},
{
"type":"view",
"name":"子按钮",
"url":"链接"
}]
}]
}';
$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_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
if (!empty($data)){
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);
curl_close($curl);
return $output;
}