微信公众号开发之定义菜单

微信订阅号可以直接在微信后台直接进行自定义菜单设置,由于开发者主要是使用微信公众号测试号进行开发,所以用代码进行自定义菜单就尤为重要,上代码:


header("Content-type: text/html; charset=utf-8");
define("ACCESS_TOKEN", "ZnN6wroZM7kOAbzYuFv6R1gCQoDDvByqdMlTDkUsa0Hp6zUIE6Svkhc_Nc-VPSTq7QBqvAYRI_DYAnynKtrKMUjlbCzEYph8Lci96ZRCt8m94f_eKWISRYQlCG9-KGzwUJIbAJAJYX");


//创建菜单
function createMenu($data){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".ACCESS_TOKEN);
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;

}

//获取菜单
function getMenu(){
return file_get_contents("https://api.weixin.qq.com/cgi-bin/menu/get?access_token=".ACCESS_TOKEN);
}

//删除菜单
function deleteMenu(){
return file_get_contents("https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=".ACCESS_TOKEN);
}





$data = '{
     "button":[
     {
          "type":"view",
          "name":"替换为你的菜单名称",
          "url":"跳转到你自定义的外部链接地址"
      },
      {
           "type":"click",
           "name":"替换为你的菜单名称",
           "key":"#"
      },
      ]
}';




echo createMenu($data);
//echo getMenu();
//echo deleteMenu();

直接在浏览器调用改程序即可得到菜单,执行之后微信公众号可能延迟显示,这里有一个小技巧就是直接取消对公众号的关注然后重新关注即可看到被刷新的内容。

你可能感兴趣的:(微信公众号开发)