微信自定义菜单
创建接口
https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN
删除接口
https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=ACCESS_TOKEN微信自定义菜单的菜单体,我开始做的时候我是直接把数据从数据库读出来,然后弄到一个数组中json一下,但这样做却报错,
40033 | 不合法的请求字符,不能包含\uxxxx格式的字符 |
/*
*创建菜单
*/
public function create_menu($menu, $access_token)
{
//https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN
$url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$access_token;
$result = $this->get_response_post($url, $menu);
$res = json_decode($result, true);
return $res['errcode'];
}
public function del_menu($access_token)
{
//https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=ACCESS_TOKEN
$url = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=".$access_token;
$response = file_get_contents($url);
$res = json_decode($response, true);
return $res['errcode'];
}
elseif($act == "create")
{
$menuArr = array();
$access_token = $hcwechat->get_access_token();
$menu = $op->get_menu(0,3);
$big_count = $op->have_menu(0);
$i = 1;
$data = '{"button":[';
foreach ($menu as $key => $val)
{
$child = $op->get_menu($val['id'],5);
$small_count = $op->have_menu($val['id']);
if(empty($child))
{
$name = $val['name'];
if($val['event'] != "")
{
$type = "click";
$key = $val['event'];
$data.='{"type":"'.$type.'",';
$data.='"name":"'.$name.'",';
$data.='"key":"'.$key.'"}';
}else
{
$type = "view";
$url = $val['url'];
$data.='{"type":"'.$type.'",';
$data.='"name":"'.$name.'",';
$data.='"url":"'.$url.'"}';
}
}else
{
$name = $val['name'];
$data.='{"name":"'.$name.'",';
$data.='"sub_button":[';
$j = 1;
foreach ($child as $k => $v)
{
$name = $v['name'];
if($v['event'] != "")
{
$type = "click";
$key = $v['event'];
$data.='{"type":"'.$type.'",';
$data.='"name":"'.$name.'",';
$data.='"key":"'.$key.'"}';
}else
{
$type = "view";
$url = $v['url'];
$data.='{"type":"'.$type.'",';
$data.='"url":"'.$url.'",';
<pre code_snippet_id="662464" snippet_file_name="blog_20150509_2_5841886" name="code" class="php"><span style="white-space:pre"> </span>$data.='"url":"'.$url.'"}';
}if($j==$small_count){$data.='';}else{$data.=',';}$j++;}$data.=']}';}if($i==$big_count){$data.='';}else{$data.=',';}$i++;}$data.=']}';//$menu = json_encode($menuArr,true);$hcwechat->del_menu($access_token);//删除已有菜单$res = $hcwechat->create_menu($data, $access_token);//echo $res;//echo $data;if($res == 0){echo "<script>alert('创建成功');</script>";echo "<script>window.location.href='menu.php';</script>";exit;}else{echo "<script>alert('创建失败".$res."');</script>";echo "<script>window.location.href='menu.php';</script>";exit;}}
微信规定 : 自定义菜单最多包括3个一级菜单,每个一级菜单最多包含5个二级菜单
function get_response_post($url, $data){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$tmpInfo = curl_exec($ch);
if (curl_errno($ch)) {
return curl_error($ch);
}
curl_close($ch);
return $tmpInfo;
}
所以读取数据时最好做一个数量限制;