自定义微信公众号菜单一二级选项(一)

微信公众号的后台给我们提供了编辑菜单、预览、自动回复等快捷功能。但是假如我们若想获取公众号里面的内容,则必须启用

服务器配置。

  但是问题来了。启用服务器配置后,微信会告诉我们自动回复和自定义菜单功能失效了。看了微信的文档,但是说的不是很详细,不知道在哪里写代码
我相信不止我一个人遇到这个问题,经过几天的研究终于搞清楚了,下面我简单介绍一下:
创建一个php文件:

<?php

//获取token
require_once "lib/jssdk.php";


$GLOBALS["CurWeixinMch"] = 2; // Weixin Mch for JSAPI
require_once(dirname(__FILE__) . "/WxPayPubHelper/WxPay.pub.config.php");


$jssdk = new JSSDK($GLOBALS["WxPayConfig"]["APPID"], $GLOBALS["WxPayConfig"]["APPSECRET"], false);


$access_token = $jssdk->getAccessToken();


$jsonmenu = '{
      "button":[
      {
           "name":"销售",
           "sub_button":[
             {
                "type": "click", 
                "name": "新车推荐", 
                "key": "推荐新车"
            }, 
            {
               "type":"view",
                "name":"预约试驾",
                "url":"http://oliveche.com/zhanda/weixin/index.php?entry=?svrType=DR%23plugin_testDrive-drive"
            },
             {
                "type": "click", 
                "name": "商城", 
                "key": "城商"
            }, 
             {
                "type": "click", 
                "name": "关于我们", 
                "key": "我们关于"
            }]
       },
       {
           "name":"售后",
           "sub_button":[
            {
               "type":"view",
                "name":"保养预约",
                "url":"http://oliveche.com/zhanda/weixin/index.php?entry=?svrType=SV%23plugin_maintain-maintainces"
            },
            {
               "type":"view",
                "name":"维修预约",
                "url":"http://oliveche.com/zhanda/weixin/index.php?entry=?svrType=MA%23plugin_sprayPaint-metalServiceStar"
            },
            {
                "type":"view",
                "name":"钣金喷漆",
                "url":"http://oliveche.com/zhanda/weixin/index.php?entry=?svrType=SP%23plugin_sprayPaint-metalServiceStar"
            },     
            {
                "type": "click", 
                "name": "我要续保", 
                "key": "续保"
            },
            {
                "type": "click", 
                "name": "道路救援", 
                "key": "救援"
            }]
       


       },
       {
           "name":"关于我们",
           "sub_button":[
            {
                "type":"view",
                "name":"总经理工作台",
                "url":"http://oliveche.com/zhanda/weixin/index.php?entry=?svrType=MA%23managerView"
            },
            {
                "type": "click", 
                "name": "联系我们", 
                "key": "key_aboutUs"
            },
            {
                "type": "click", 
                "name": "产品服务简介", 
                "key": "简介"
            },
            {
               "type":"view",
                "name":"会员中心",
                "url":"http://oliveche.com/zhanda/weixin/index.php?entry=myCard"
            },
            {
                "type":"view",
                "name":"用户中心",
                "url":"http://oliveche.com/zhanda/weixin/index.php?entry=?svrType=MA#me"
            }]
      


       }]
 }';




$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;
}

?>

PHP 手动在浏览器运行这个文件,则你就会发现公众号里面的一二级菜单变了。
体验到成功的喜悦了。

关于菜单按钮的点击事件和获取消息内容,我将会在下篇博客中写到。

猜你喜欢

转载自blog.csdn.net/ywcsd/article/details/52808353