破晓 发表于 2016-8-18 15:32:18

微信公众平台开发(八) 自定义菜单功能开发

来源:http://www.cnblogs.com/mchina/p/3276878.html<p></p><div><br></div><div><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><span style="line-height: 1.8; font-size: 16px;"><strong><span style="line-height: 1.8; color: rgb(128, 0, 0);">一、简介</span></strong></span></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">微信公众平台<strong>服务号</strong>以及<strong>之前成功申请内测资格的订阅号</strong>都具有自定义菜单的功能。开发者可利用该功能为公众账号的会话界面底部增加自定义菜单,用户点击菜单中的选项,可以调出相应的回复信息或网页链接。自定义菜单接口将为公众账号的信息展示空间提供更多可能性。本文将针对自定义菜单做简单的开发应用,<span style="line-height: 25px; orphans: 2; widows: 2; font-stretch: normal;">以供读者参考。</span></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><span style="line-height: 1.8; font-size: 16px;"><strong><span style="line-height: 1.8; color: rgb(128, 0, 0);">二、官方说明</span></strong></span></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">开发者获取使用凭证后,可以使用该凭证对公众账号的自定义菜单进行创建、查询和删除等操作。 自定义菜单接口可实现以下类型按钮:</p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">click(点击事件):</p><p style="margin: 10px auto 10px 30px; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">用户点击click类型按钮后,微信服务器会通过消息接口(event类型)推送点击事件给开发者,并且带上按钮中开发者填写的key值,开发者可以通过自定义的key值进行消息回复。</p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">view(访问网页):</p><p style="margin: 10px auto 10px 30px; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">用户点击view类型按钮后,会直接跳转到开发者指定的url中。</p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">创建自定义菜单后,由于微信客户端缓存,需要24小时微信客户端才会展现出来。建议测试时可以尝试取消关注公众账号后,再次关注,则可以看到创建后的效果。</p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">文档地址:<a href="http://mp.weixin.qq.com/wiki/index.php?title=%E8%87%AA%E5%AE%9A%E4%B9%89%E8%8F%9C%E5%8D%95%E6%8E%A5%E5%8F%A3" style="text-decoration: none; color: rgb(0, 0, 0); border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: rgb(51, 51, 51);">http://mp.weixin.qq.com/wiki/index.php?title=%E8%87%AA%E5%AE%9A%E4%B9%89%E8%8F%9C%E5%8D%95%E6%8E%A5%E5%8F%A3</a></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><span style="line-height: 1.8; font-size: 16px;"><strong><span style="line-height: 1.8; color: rgb(128, 0, 0);">三、获取使用凭证</span></strong></span></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><strong>3.1 获取appid 和appsecret</strong></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">在 微信公众平台 &gt; 高级功能 &gt; 开发模式中找到appid&nbsp;和appsecret。</p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><img src="http://images.cnitblog.com/blog/370046/201308/25173106-75e802be76c84fb9af892f42c4490111.jpg" alt="" style="border: 0px; max-width: 900px;">&nbsp;</p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><strong>3.2 使用appid 和appsecret 向微信凭证获取接口请求access_token</strong></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">请求地址:<a href="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&amp;appid=APPID&amp;secret=APPSECRET" target="_blank" style="text-decoration: none; color: rgb(0, 0, 0); border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: rgb(51, 51, 51);">https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&amp;appid=APPID&amp;secret=APPSECRET</a></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">请求参数说明:</p><p style="margin: 10px auto 10px 30px; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">grant_type:<span style="line-height: 1.8;">获取access_token填写client_credential</span></p><p style="margin: 10px auto 10px 30px; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">appid:<span style="line-height: 1.8;">第三方用户唯一凭证</span></p><p style="margin: 10px auto 10px 30px; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">secret:<span style="line-height: 1.8;">第三方用户唯一凭证密钥,既appsecret</span></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><span style="line-height: 1.8;">返回说明:</span></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><span style="line-height: 1.8;">正确的Json返回结果:</span></p><div class="cnblogs_code" style="margin-top: 5px; margin-bottom: 5px; padding: 5px; border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(0, 0, 0); font-family: &quot;Courier New&quot; !important; font-size: 12px !important; background-color: rgb(245, 245, 245);"><pre style="white-space: pre-wrap; font-family: &quot;Courier New&quot; !important;">{"access_token":"ACCESS_TOKEN","expires_in":7200}</pre></div><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">返回参数说明:</p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">access_token:获取到的凭证</p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">expires_in:凭证有效时间,单位:秒</p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><strong>3.3 具体实现</strong></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">a. 打印出格式</p><div class="cnblogs_code" style="margin-top: 5px; margin-bottom: 5px; padding: 5px; border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(0, 0, 0); font-family: &quot;Courier New&quot; !important; font-size: 12px !important; background-color: rgb(245, 245, 245);"><div class="cnblogs_code_toolbar" style="margin-top: 5px;"><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.8;"><a title="复制代码" style="color: rgb(0, 0, 0); border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="max-width: 900px;"></a></span></div><pre style="white-space: pre-wrap; font-family: &quot;Courier New&quot; !important;">&lt;?<span style="line-height: 1.8;">php

</span><span style="line-height: 1.8; color: rgb(128, 0, 128);">$APPID</span>="wxdxxxxxxxxxxxxxxx"<span style="line-height: 1.8;">;
</span><span style="line-height: 1.8; color: rgb(128, 0, 128);">$APPSECRET</span>="96xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"<span style="line-height: 1.8;">;

</span><span style="line-height: 1.8; color: rgb(128, 0, 128);">$TOKEN_URL</span>="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&amp;appid=".<span style="line-height: 1.8; color: rgb(128, 0, 128);">$APPID</span>."&amp;secret=".<span style="line-height: 1.8; color: rgb(128, 0, 128);">$APPSECRET</span><span style="line-height: 1.8;">;

</span><span style="line-height: 1.8; color: rgb(128, 0, 128);">$json</span>=<span style="line-height: 1.8; color: rgb(0, 128, 128);">file_get_contents</span>(<span style="line-height: 1.8; color: rgb(128, 0, 128);">$TOKEN_URL</span><span style="line-height: 1.8;">);
</span><span style="line-height: 1.8; color: rgb(128, 0, 128);">$result</span>=json_decode(<span style="line-height: 1.8; color: rgb(128, 0, 128);">$json</span>,<span style="line-height: 1.8; color: rgb(0, 0, 255);">true</span><span style="line-height: 1.8;">);
</span><span style="line-height: 1.8; color: rgb(0, 128, 128);">print_r</span>(<span style="line-height: 1.8; color: rgb(128, 0, 128);">$result</span><span style="line-height: 1.8;">);

</span>?&gt;</pre><div class="cnblogs_code_toolbar" style="margin-top: 5px;"><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.8;"><a title="复制代码" style="color: rgb(0, 0, 0); border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="max-width: 900px;"></a></span></div></div><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">结果如下:</p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><img src="http://images.cnitblog.com/blog/370046/201308/25181102-58b16eb402554136bedccffa461c396c.jpg" alt="" width="900" style="border: 0px; max-width: 900px;"></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">b. 获取access_token</p><div class="cnblogs_code" style="margin-top: 5px; margin-bottom: 5px; padding: 5px; border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(0, 0, 0); font-family: &quot;Courier New&quot; !important; font-size: 12px !important; background-color: rgb(245, 245, 245);"><div class="cnblogs_code_toolbar" style="margin-top: 5px;"><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.8;"><a title="复制代码" style="color: rgb(0, 0, 0); border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="max-width: 900px;"></a></span></div><pre style="white-space: pre-wrap; font-family: &quot;Courier New&quot; !important;">&lt;?<span style="line-height: 1.8;">php

</span><span style="line-height: 1.8; color: rgb(128, 0, 128);">$APPID</span>="wxdxxxxxxxxxxxxxxx"<span style="line-height: 1.8;">;
</span><span style="line-height: 1.8; color: rgb(128, 0, 128);">$APPSECRET</span>="96xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"<span style="line-height: 1.8;">;

</span><span style="line-height: 1.8; color: rgb(128, 0, 128);">$TOKEN_URL</span>="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&amp;appid=".<span style="line-height: 1.8; color: rgb(128, 0, 128);">$APPID</span>."&amp;secret=".<span style="line-height: 1.8; color: rgb(128, 0, 128);">$APPSECRET</span><span style="line-height: 1.8;">;

</span><span style="line-height: 1.8; color: rgb(128, 0, 128);">$json</span>=<span style="line-height: 1.8; color: rgb(0, 128, 128);">file_get_contents</span>(<span style="line-height: 1.8; color: rgb(128, 0, 128);">$TOKEN_URL</span><span style="line-height: 1.8;">);
</span><span style="line-height: 1.8; color: rgb(128, 0, 128);">$result</span>=json_decode(<span style="line-height: 1.8; color: rgb(128, 0, 128);">$json</span>,<span style="line-height: 1.8; color: rgb(0, 0, 255);">true</span><span style="line-height: 1.8;">);

</span><span style="line-height: 1.8; color: rgb(128, 0, 128);">$ACC_TOKEN</span>=<span style="line-height: 1.8; color: rgb(128, 0, 128);">$result</span>['access_token'<span style="line-height: 1.8;">];

</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">echo</span> <span style="line-height: 1.8; color: rgb(128, 0, 128);">$ACC_TOKEN</span><span style="line-height: 1.8;">;

</span>?&gt;</pre><div class="cnblogs_code_toolbar" style="margin-top: 5px;"><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.8;"><a title="复制代码" style="color: rgb(0, 0, 0); border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="max-width: 900px;"></a></span></div></div><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><img src="http://images.cnitblog.com/blog/370046/201308/25181332-8436fad05a5148e08acd35c8e51029f4.jpg" alt="" width="900" style="border: 0px; max-width: 900px;"></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">注:access_token对应于公众号是全局唯一的票据,重复获取将导致上次获取的access_token失效。&nbsp;</p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><span style="line-height: 1.8; font-size: 16px;"><strong><span style="line-height: 1.8; color: rgb(128, 0, 0);">四、创建菜单</span></strong></span></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><span style="line-height: 1.8;"><strong>方法:</strong>通过POST一个特定结构体,实现在微信客户端创建自定义菜单。</span></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><span style="line-height: 1.8;">请求地址:<a href="https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN" target="_blank" style="text-decoration: none; color: rgb(0, 0, 0); border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: rgb(51, 51, 51);">https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN</a></span></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><span style="line-height: 1.5;">请求示例:</span></p><div class="cnblogs_code" style="margin-top: 5px; margin-bottom: 5px; padding: 5px; border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(0, 0, 0); font-family: &quot;Courier New&quot; !important; font-size: 12px !important; background-color: rgb(245, 245, 245);"><div class="cnblogs_code_toolbar" style="margin-top: 5px;"><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.8;"><a title="复制代码" style="color: rgb(0, 0, 0); border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="max-width: 900px;"></a></span></div><pre style="white-space: pre-wrap; font-family: &quot;Courier New&quot; !important;"><span style="line-height: 1.8;">{
"button":[
{
       "name":"公共查询",
       "sub_button":[
      {
         "type":"click",
         "name":"天气查询",
         "key":"tianQi"
      },
      {
         "type":"click",
         "name":"公交查询",
         "key":"gongJiao"
      },
      {
         "type":"click",
         "name":"翻译",
         "key":"fanYi"
      }]
},
{
       "name":"苏州本地",
       "sub_button":[
      {
         "type":"click",
         "name":"爱上苏州",
         "key":"loveSuzhou"
      },
      {
         "type":"click",
         "name":"苏州景点",
         "key":"suzhouScenic"
      },
      {
         "type":"click",
         "name":"苏州美食",
         "key":"suzhouFood"
      },
      {
         "type":"click",
         "name":"住在苏州",
         "key":"liveSuzhou"
      }]
   },
   {
       "type":"click",
       "name":"联系我们",
       "key":"lianxiUs"
   }]
}</span></pre><div class="cnblogs_code_toolbar" style="margin-top: 5px;"><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.8;"><a title="复制代码" style="color: rgb(0, 0, 0); border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="max-width: 900px;"></a></span></div></div><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">示例说明:</p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">菜单结构及说明:</p><div class="cnblogs_code" style="margin-top: 5px; margin-bottom: 5px; padding: 5px; border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(0, 0, 0); font-family: &quot;Courier New&quot; !important; font-size: 12px !important; background-color: rgb(245, 245, 245);"><div class="cnblogs_code_toolbar" style="margin-top: 5px;"><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.8;"><a title="复制代码" style="color: rgb(0, 0, 0); border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="max-width: 900px;"></a></span></div><pre style="white-space: pre-wrap; font-family: &quot;Courier New&quot; !important;"><span style="line-height: 1.8;">{
"button":[             //button定义该结构为一个菜单
{
       "name":"分支主菜单名",  
       "sub_button":[    //sub_button定义子菜单
      {
         "type":"click",  //按钮类型
         "name":"分支子菜单名1",  //菜单名称
         "key":"loveSuzhou"  //菜单key值
      },
      {
         "type":"click",
         "name":"分支子菜单名2",
         "key":"liveSuzhou"
      }]
   },    //菜单之间用 , 分隔
   {
       "type":"click",
       "name":"独立菜单",
       "key":"lianxiUs"
   }]
}</span></pre><div class="cnblogs_code_toolbar" style="margin-top: 5px;"><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.8;"><a title="复制代码" style="color: rgb(0, 0, 0); border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="max-width: 900px;"></a></span></div></div><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">返回说明:</p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><span style="line-height: 1.8;">正确的Json返回结果:</span></p><div class="cnblogs_code" style="margin-top: 5px; margin-bottom: 5px; padding: 5px; border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(0, 0, 0); font-family: &quot;Courier New&quot; !important; font-size: 12px !important; background-color: rgb(245, 245, 245);"><pre style="white-space: pre-wrap; font-family: &quot;Courier New&quot; !important;">{"errcode":0,"errmsg":"ok"}</pre></div><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">提交菜单:</p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">通过curl 提交以上的菜单数据,代码如下:</p><div class="cnblogs_code" style="margin-top: 5px; margin-bottom: 5px; padding: 5px; border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(0, 0, 0); font-family: &quot;Courier New&quot; !important; font-size: 12px !important; background-color: rgb(245, 245, 245);"><div class="cnblogs_code_toolbar" style="margin-top: 5px;"><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.8;"><a title="复制代码" style="color: rgb(0, 0, 0); border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="max-width: 900px;"></a></span></div><pre style="white-space: pre-wrap; font-family: &quot;Courier New&quot; !important;"><span style="line-height: 1.8; color: rgb(128, 0, 128);">$MENU_URL</span>="https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".<span style="line-height: 1.8; color: rgb(128, 0, 128);">$ACC_TOKEN</span><span style="line-height: 1.8;">;

</span><span style="line-height: 1.8; color: rgb(128, 0, 128);">$ch</span> =<span style="line-height: 1.8;"> curl_init();

curl_setopt(</span><span style="line-height: 1.8; color: rgb(128, 0, 128);">$ch</span>, CURLOPT_URL, <span style="line-height: 1.8; color: rgb(128, 0, 128);">$MENU_URL</span><span style="line-height: 1.8;">);
curl_setopt(</span><span style="line-height: 1.8; color: rgb(128, 0, 128);">$ch</span>, CURLOPT_CUSTOMREQUEST, "POST"<span style="line-height: 1.8;">);
curl_setopt(</span><span style="line-height: 1.8; color: rgb(128, 0, 128);">$ch</span>, CURLOPT_SSL_VERIFYPEER, <span style="line-height: 1.8; color: rgb(0, 0, 255);">FALSE</span><span style="line-height: 1.8;">);
curl_setopt(</span><span style="line-height: 1.8; color: rgb(128, 0, 128);">$ch</span>, CURLOPT_SSL_VERIFYHOST, <span style="line-height: 1.8; color: rgb(0, 0, 255);">FALSE</span><span style="line-height: 1.8;">);
curl_setopt(</span><span style="line-height: 1.8; color: rgb(128, 0, 128);">$ch</span>, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)'<span style="line-height: 1.8;">);
curl_setopt(</span><span style="line-height: 1.8; color: rgb(128, 0, 128);">$ch</span>, CURLOPT_FOLLOWLOCATION, 1<span style="line-height: 1.8;">);
curl_setopt(</span><span style="line-height: 1.8; color: rgb(128, 0, 128);">$ch</span>, CURLOPT_AUTOREFERER, 1<span style="line-height: 1.8;">);
curl_setopt(</span><span style="line-height: 1.8; color: rgb(128, 0, 128);">$ch</span>, CURLOPT_POSTFIELDS, <span style="line-height: 1.8; color: rgb(128, 0, 128);">$data</span><span style="line-height: 1.8;">);
curl_setopt(</span><span style="line-height: 1.8; color: rgb(128, 0, 128);">$ch</span>, CURLOPT_RETURNTRANSFER, <span style="line-height: 1.8; color: rgb(0, 0, 255);">true</span><span style="line-height: 1.8;">);

</span><span style="line-height: 1.8; color: rgb(128, 0, 128);">$info</span> = curl_exec(<span style="line-height: 1.8; color: rgb(128, 0, 128);">$ch</span><span style="line-height: 1.8;">);

</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">if</span> (curl_errno(<span style="line-height: 1.8; color: rgb(128, 0, 128);">$ch</span><span style="line-height: 1.8;">)) {
    </span><span style="line-height: 1.8; color: rgb(0, 0, 255);">echo</span> 'Errno'.curl_error(<span style="line-height: 1.8; color: rgb(128, 0, 128);">$ch</span><span style="line-height: 1.8;">);
}

curl_close(</span><span style="line-height: 1.8; color: rgb(128, 0, 128);">$ch</span><span style="line-height: 1.8;">);

</span><span style="line-height: 1.8; color: rgb(0, 128, 128);">var_dump</span>(<span style="line-height: 1.8; color: rgb(128, 0, 128);">$info</span>);</pre><div class="cnblogs_code_toolbar" style="margin-top: 5px;"><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.8;"><a title="复制代码" style="color: rgb(0, 0, 0); border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="max-width: 900px;"></a></span></div></div><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">生成菜单:</p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">创建菜单的代码提交到服务器后,并不是自动就出来了,需要通过执行该代码文件才能生成,所以,打开浏览器,在地址栏里输入完整的菜单代码URL,运行结果如下:</p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><img src="http://images.cnitblog.com/blog/370046/201308/25195348-7ba2fc2dd7d844ec8b13bac103ea2a90.jpg" alt="" style="border: 0px; max-width: 900px;"></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">测试结果:</p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><img src="http://images.cnitblog.com/blog/370046/201308/25210832-c950609ed6924b93b1bb34ef36f09912.png" alt="" style="border: 0px; max-width: 900px;"></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">菜单创建成功。</p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><span style="line-height: 1.8; font-size: 16px;"><strong><span style="line-height: 1.8; color: rgb(128, 0, 0);">五、查询菜单</span></strong></span></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><span style="line-height: 1.8;">查询当前使用的自定义菜单结构。</span></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">请求地址:<a href="https://api.weixin.qq.com/cgi-bin/menu/get?access_token=ACCESS_TOKEN" target="_blank" style="text-decoration: none; color: rgb(0, 0, 0); border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: rgb(51, 51, 51);">https://api.weixin.qq.com/cgi-bin/menu/get?access_token=ACCESS_TOKEN</a></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">curl 代码如下:</p><div class="cnblogs_code" style="margin-top: 5px; margin-bottom: 5px; padding: 5px; border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(0, 0, 0); font-family: &quot;Courier New&quot; !important; font-size: 12px !important; background-color: rgb(245, 245, 245);"><div class="cnblogs_code_toolbar" style="margin-top: 5px;"><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.8;"><a title="复制代码" style="color: rgb(0, 0, 0); border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="max-width: 900px;"></a></span></div><pre style="white-space: pre-wrap; font-family: &quot;Courier New&quot; !important;"><span style="line-height: 1.8; color: rgb(128, 0, 128);">$MENU_URL</span>="https://api.weixin.qq.com/cgi-bin/menu/get?access_token=".<span style="line-height: 1.8; color: rgb(128, 0, 128);">$ACC_TOKEN</span><span style="line-height: 1.8;">;

</span><span style="line-height: 1.8; color: rgb(128, 0, 128);">$cu</span> =<span style="line-height: 1.8;"> curl_init();
curl_setopt(</span><span style="line-height: 1.8; color: rgb(128, 0, 128);">$cu</span>, CURLOPT_URL, <span style="line-height: 1.8; color: rgb(128, 0, 128);">$MENU_URL</span><span style="line-height: 1.8;">);
curl_setopt(</span><span style="line-height: 1.8; color: rgb(128, 0, 128);">$cu</span>, CURLOPT_RETURNTRANSFER, 1<span style="line-height: 1.8;">);
</span><span style="line-height: 1.8; color: rgb(128, 0, 128);">$menu_json</span> = curl_exec(<span style="line-height: 1.8; color: rgb(128, 0, 128);">$cu</span><span style="line-height: 1.8;">);
</span><span style="line-height: 1.8; color: rgb(128, 0, 128);">$menu</span> = json_decode(<span style="line-height: 1.8; color: rgb(128, 0, 128);">$menu_json</span><span style="line-height: 1.8;">);
curl_close(</span><span style="line-height: 1.8; color: rgb(128, 0, 128);">$cu</span><span style="line-height: 1.8;">);

</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">echo</span> <span style="line-height: 1.8; color: rgb(128, 0, 128);">$menu_json</span>;</pre><div class="cnblogs_code_toolbar" style="margin-top: 5px;"><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.8;"><a title="复制代码" style="color: rgb(0, 0, 0); border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="max-width: 900px;"></a></span></div></div><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><span style="line-height: 1.5;">运行结果:</span></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><span style="line-height: 1.5;"><img src="http://images.cnitblog.com/blog/370046/201308/25205128-607538a8b0ad41e6956c6eb50e2b2383.jpg" alt="" width="900" style="border: 0px; max-width: 900px;"></span></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><span style="line-height: 1.5;">菜单查询成功。</span></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><span style="line-height: 1.8; font-size: 16px;"><strong><span style="line-height: 1.5; color: rgb(128, 0, 0);">六、删除菜单</span></strong></span></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><span style="line-height: 1.5;"><span style="line-height: 1.8;">取消当前使用的自定义菜单。</span></span></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">请求地址:<a href="https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=ACCESS_TOKEN" target="_blank" style="text-decoration: none; color: rgb(0, 0, 0); border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: rgb(51, 51, 51);">https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=ACCESS_TOKEN</a></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">curl 代码如下:</p><div class="cnblogs_code" style="margin-top: 5px; margin-bottom: 5px; padding: 5px; border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(0, 0, 0); font-family: &quot;Courier New&quot; !important; font-size: 12px !important; background-color: rgb(245, 245, 245);"><div class="cnblogs_code_toolbar" style="margin-top: 5px;"><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.8;"><a title="复制代码" style="color: rgb(0, 0, 0); border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="max-width: 900px;"></a></span></div><pre style="white-space: pre-wrap; font-family: &quot;Courier New&quot; !important;"><span style="line-height: 1.8; color: rgb(128, 0, 128);">$MENU_URL</span>="https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=".<span style="line-height: 1.8; color: rgb(128, 0, 128);">$ACC_TOKEN</span><span style="line-height: 1.8;">;

</span><span style="line-height: 1.8; color: rgb(128, 0, 128);">$cu</span> =<span style="line-height: 1.8;"> curl_init();
curl_setopt(</span><span style="line-height: 1.8; color: rgb(128, 0, 128);">$cu</span>, CURLOPT_URL, <span style="line-height: 1.8; color: rgb(128, 0, 128);">$MENU_URL</span><span style="line-height: 1.8;">);
curl_setopt(</span><span style="line-height: 1.8; color: rgb(128, 0, 128);">$cu</span>, CURLOPT_RETURNTRANSFER, 1<span style="line-height: 1.8;">);
</span><span style="line-height: 1.8; color: rgb(128, 0, 128);">$info</span> = curl_exec(<span style="line-height: 1.8; color: rgb(128, 0, 128);">$cu</span><span style="line-height: 1.8;">);
</span><span style="line-height: 1.8; color: rgb(128, 0, 128);">$res</span> = json_decode(<span style="line-height: 1.8; color: rgb(128, 0, 128);">$info</span><span style="line-height: 1.8;">);
curl_close(</span><span style="line-height: 1.8; color: rgb(128, 0, 128);">$cu</span><span style="line-height: 1.8;">);

</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">if</span>(<span style="line-height: 1.8; color: rgb(128, 0, 128);">$res</span>-&gt;errcode == "0"<span style="line-height: 1.8;">){
    </span><span style="line-height: 1.8; color: rgb(0, 0, 255);">echo</span> "菜单删除成功"<span style="line-height: 1.8;">;
}</span><span style="line-height: 1.8; color: rgb(0, 0, 255);">else</span><span style="line-height: 1.8;">{
    </span><span style="line-height: 1.8; color: rgb(0, 0, 255);">echo</span> "菜单删除失败"<span style="line-height: 1.8;">;
}</span></pre><div class="cnblogs_code_toolbar" style="margin-top: 5px;"><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.8;"><a title="复制代码" style="color: rgb(0, 0, 0); border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="max-width: 900px;"></a></span></div></div><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">运行结果:</p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><img src="http://images.cnitblog.com/blog/370046/201308/25205505-f81daa206c4e4b988378988fd9829f51.jpg" alt="" style="border: 0px; max-width: 900px;"></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">测试结果:</p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><img src="http://images.cnitblog.com/blog/370046/201308/25210914-709b633c02b74d98896a70ff371eb97d.png" alt="" style="border: 0px; max-width: 900px;"></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">菜单删除成功。</p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><span style="line-height: 1.8; color: rgb(128, 0, 0); font-size: 16px;"><strong>七、完整代码获取</strong></span></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><span style="line-height: 1.8;">请访问&nbsp;<a href="http://pan.baidu.com/s/1c0s3Jby" target="_blank" style="text-decoration: none; color: rgb(0, 0, 0); border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: rgb(51, 51, 51);"><span style="line-height: 1.8;"><strong>乐思乐享</strong></span></a>&nbsp;官方网盘</span></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><span style="line-height: 1.8;"><strong>URL</strong>:</span><a href="http://pan.baidu.com/s/1c0s3Jby" target="_blank" style="text-decoration: none; color: rgb(0, 0, 0); border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: rgb(51, 51, 51);">http://pan.baidu.com/s/1c0s3Jby</a></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><span style="line-height: 1.8; color: rgb(128, 0, 0); font-size: 16px;"><strong>八、关注</strong></span></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">请关注<span style="line-height: 1.8;">&nbsp;</span><span style="line-height: 1.8; color: rgb(128, 0, 0);"><strong>卓锦苏州</strong>&nbsp;</span>微信公众帐号,<span style="line-height: 1.8; color: rgb(128, 0, 0);"><strong>卓锦苏州</strong></span>&nbsp;基于BAE 平台开发,针对于主流的微信功能进行开发测试。</p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">您可以关注&nbsp;<span style="line-height: 1.8;"><span style="line-height: 1.8; color: rgb(128, 0, 0);"><strong>卓锦苏州</strong></span>&nbsp;</span>公众帐号进行功能测试,以及获取新的应用开发。</p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">1. 登录微信客户端,通讯录 -&gt; 添加朋友 -&gt; 查找公众号 -&gt;<span style="line-height: 1.8; color: rgb(128, 0, 0);">&nbsp;<strong>zhuojinsz</strong></span>,查找并关注。</p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">2. 扫描二维码:</p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><img src="http://images.cnitblog.com/blog/370046/201310/07160054-359dcfa75fd34464bc6d8d8224a700f5.jpg" alt="" style="border: 0px; max-width: 900px;"></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><span style="line-height: 1.8; color: rgb(128, 0, 0);"><strong>卓锦苏州</strong></span><span style="line-height: 1.8;"><span style="line-height: 1.8; color: rgb(128, 0, 0);">&nbsp;</span>菜单功能:</span></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><img src="http://images.cnitblog.com/blog/370046/201308/25211408-247cb00262bb449cb8c44a1aaea5d0d5.png" alt="" style="border: 0px; max-width: 900px;"></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;">&nbsp;</p><hr style="color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><span style="line-height: 1.8; color: rgb(128, 0, 0);"><strong>David Camp</strong></span></p><p style="margin: 10px auto; color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25.2px;"><span style="line-height: 1.8; color: rgb(128, 0, 0);"><strong><strong><strong>我们永远相信,分享是一种美德 |&nbsp;</strong></strong>We Believe, Great People Share Knowledge...</strong></span></p></div>
页: [1]
查看完整版本: 微信公众平台开发(八) 自定义菜单功能开发