config.php 如下:
ini_set('error_reporting', E_ALL | E_STRICT);
ini_set('display_errors', 'Off');define(AppId, "wxf123456789123456");//定义AppId,需要在微信公众平台申请自定义菜单后会得到
define(AppSecret, "6f999a999d075c5999b43940ca713b5e");//定义AppSecret,需要在微信公众平台申请自定义菜单后会得到
menu_create.php 自定义菜单创建文件
require("config.php");
function getAccessToken() //获取access_token
{
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".AppId."&secret=".AppSecret;
$data = getCurl($url);//通过自定义函数getCurl得到https的内容
$resultArr = json_decode($data, true);//转为数组
return $resultArr["access_token"];//获取access_token
}
function getCurl($url){//get https的内容
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);//不输出内容
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$result = curl_exec($ch);
curl_close ($ch);
return $result;
}
$access_taken=getAccessToken();
echo $access_taken;
function creatmsg($token)//创建菜单
{
$accessToken =$token;//获取access_token
$menuPostString ='
{
"button":[
{
"name":"精彩活动",
"sub_button":[
{
"type": "view",
"name": "迈腾展厅开放日活动",
"url": "http://www.abc.com/wxv3/mtbm.php?cid=2",
"sub_button": [ ]
},
{
"type": "view",
"name": "展厅抽奖活动",
"url": "http://newgolf.abc.cn",
"sub_button": [ ]
},
{
"type": "view",
"name": "惊喜大放送",
"url": "http://mini1.abc.cn/",
"sub_button": [ ]
},
{
"type": "view",
"name": "返回首页",
"url": "http://www.abc.com/wxv3/index.php?cid=1",
"sub_button": [ ]
} ]
},{
"name":"我要买车",
"sub_button":[
{
"type": "view",
"name": "我要买车",
"url": "http://www.abc.com/wxv3/xcxs.php?cid=2",
"sub_button": [ ]
},
{
"type": "view",
"name": "价格查询",
"url": "http://www.abc.com/wxv3/jgcc.php?cid=6",
"sub_button": [ ]
},
{
"type": "view",
"name": "试驾体验",
"url": "http://www.abc.com/wxv3/zxsc.php?cid=7",
"sub_button": [ ]
},
{
"type": "view",
"name": "车型查询",
"url": "http://www.abc.com/wxv3/kccx.php?cid=8",
"sub_button": [ ]
},
{
"type": "view",
"name": "我的信息",
"url": "http://www.abc.com/wxv3/wdac.php?cid=4",
"sub_button": [ ]
}]
},
{
"name":"我在用车",
"sub_button":[
{
"type": "view",
"name": "我在用车",
"url": "http://www.abc.com/wxv3/shfw.php?cid=3",
"sub_button": [ ]
},
{
"type": "view",
"name": "保养查询",
"url": "http://www.abc.com/wxv3/jcby.php?cid=20",
"sub_button": [ ]
},
{
"type": "view",
"name": "特价工时",
"url": "http://www.abc.com/wxv3/yygs.php?cid=21",
"sub_button": [ ]
},
{
"type": "view",
"name": "预约保养",
"url": "http://www.abc.com/wxv3/yyby.php?cid=22",
"sub_button": [ ]
},
{
"type": "view",
"name": "车主登录",
"url": "http://www.abc.com/wxv3/wdac.php?cid=4",
"sub_button": [ ]
}]
}
]
}
';
$menuPostUrl = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$accessToken;//POST的url\
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $menuPostUrl);//设置链接
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//设置是否返回信息
curl_setopt($ch, CURLOPT_HTTPHEADER, 0);//设置HTTP头
curl_setopt($ch, CURLOPT_POST, 1);//设置为POST方式
curl_setopt($ch, CURLOPT_POSTFIELDS, $menuPostString);//POST数据
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,false);
$response = curl_exec($ch);//接收返回信息
if(curl_errno($ch)){//出错则显示错误信息
print curl_error($ch);
}
curl_close($ch); //关闭curl链接
$resultArr = json_decode($response, true);//转为数组
echo $response;
}
creatmsg($access_taken);
menu_view.php 自定义菜单查看
require("config.php");
function getAccessToken() //获取access_token
{
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".AppId."&secret=".AppSecret;
$data = getCurl($url);//通过自定义函数getCurl得到https的内容
$resultArr = json_decode($data, true);//转为数组
return $resultArr["access_token"];//获取access_token
}
function getCurl($url){//get https的内容
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);//不输出内容
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$result = curl_exec($ch);
curl_close ($ch);
return $result;
}
$access_taken=getAccessToken();
echo $access_taken;
function creatmsg($token)//创建菜单
{
$accessToken =$token;//获取access_token
$menuPostString ='';
$menuPostUrl = "https://api.weixin.qq.com/cgi-bin/menu/get?access_token=".$accessToken;//POST的url\
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $menuPostUrl);//设置链接
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//设置是否返回信息
curl_setopt($ch, CURLOPT_HTTPHEADER, 0);//设置HTTP头
curl_setopt($ch, CURLOPT_POST, 1);//设置为POST方式
curl_setopt($ch, CURLOPT_POSTFIELDS, $menuPostString);//POST数据
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,false);
$response = curl_exec($ch);//接收返回信息
if(curl_errno($ch)){//出错则显示错误信息
print curl_error($ch);
}
curl_close($ch); //关闭curl链接
$resultArr = json_decode($response, true);//转为数组
echo $response;//获取access_token
}
creatmsg($access_taken);
menu_delete.php 自定义菜单删除
require("config.php");
function getAccessToken() //获取access_token
{
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".AppId."&secret=".AppSecret;
$data = getCurl($url);//通过自定义函数getCurl得到https的内容
$resultArr = json_decode($data, true);//转为数组
return $resultArr["access_token"];//获取access_token
}
function getCurl($url){//get https的内容
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);//不输出内容
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$result = curl_exec($ch);
curl_close ($ch);
return $result;
}
$access_taken=getAccessToken();
echo $access_taken;
function creatmsg($token)//创建菜单
{
$accessToken =$token;//获取access_token
$menuPostString ='';
$menuPostUrl = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=".$accessToken;//POST的url\
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $menuPostUrl);//设置链接
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//设置是否返回信息
curl_setopt($ch, CURLOPT_HTTPHEADER, 0);//设置HTTP头
curl_setopt($ch, CURLOPT_POST, 1);//设置为POST方式
curl_setopt($ch, CURLOPT_POSTFIELDS, $menuPostString);//POST数据
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,false);
$response = curl_exec($ch);//接收返回信息
if(curl_errno($ch)){//出错则显示错误信息
print curl_error($ch);
}
curl_close($ch); //关闭curl链接
$resultArr = json_decode($response, true);//转为数组
echo $response;
}
creatmsg($access_taken);