<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<?php
// 替换为你自己的AppID和AppSecret
$appId = 'wxd95fca43ae595a61';
$appSecret = '872a95a04a7e1bd78e3f01020ac195f1';
// 获取Access Token
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appId}&secret={$appSecret}";
$response = file_get_contents($url);
$result = json_decode($response, true);
$accessToken = $result['access_token'];
// 创建底部菜单栏的JSON结构
$menu = array(
'button' => array(
array(
'name' => '故障查询',
'sub_button' => array(
array(
'type' => 'view',
'name' => '安川',
'url' => 'https://www.example.com/submenu1',
),
array(
'type' => 'click',
'name' => '发那科',
'url' => 'https://www.example.com/submenu1',
),
),
),
array(
'name' => '故障查询',
'sub_button' => array(
array(
'type' => 'view',
'name' => '安川',
'url' => 'https://www.example.com/submenu1',
),
array(
'type' => 'click',
'name' => '发那科',
'url' => 'https://www.example.com/submenu1',
),
),
),
array(
'name' => '故障查询',
'sub_button' => array(
array(
'type' => 'view',
'name' => '安川',
'url' => 'https://www.example.com/submenu1',
),
array(
'type' => 'click',
'name' => '发那科',
'url' => 'https://www.example.com/submenu1',
),
),
),
),
);
// 将菜单结构转换为JSON字符串并进行URL编码
$data = json_encode($menu, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
$data = urlencode($data);
// 发送请求创建底部菜单栏
$url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token={$accessToken}";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, urldecode($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
// 检查请求返回结果
$result = json_decode($response, true);
if ($result['errcode'] == 0) {
echo '创建底部菜单栏成功';
} else {
echo '创建底部菜单栏失败:' . $result['errmsg'];
}
?>
以上代码运行后,显示成功,但不能创建微信公众号底部菜单栏