<pre name="code" class="php"><pre name="code" 
class="html"><pre name="code" 
class="plain">1.申请成为服务号
2.申请自定义菜单,得到appid和appsecert
3.用appid和appsecert获得access 
token
4.将菜单组织成结构,通过post提交给接口
5.在消息接口中处理event事件,其中的click代表菜单点击,通过响应菜单结构中的key值回应消息</pre><br>
<pre></pre>
<br>
<pre></pre>
<pre 
name="code" class="php">static function 
createMean($access_token){
        $url = 
"https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$access_token;
   
     $data = ‘{
       
      "button":[
       
      {
       
              
 "name":"公司信息",
           
     "sub_button":[
       
             {
   
                
 "type":"click",
           
         "name":"公司介绍",
   
                
 "key":"wx_company"
           
         },
   
                
 {
               
     "type":"click",
       
            
 "name":"联系我们",
           
         "key":"wx_contact"
   
                
 }
               
 ]
           
 },
           
 {
               
 "name":"供应产品",
           
     "sub_button":[
       
             {
   
                
 "type":"click",
           
         "name":"分类一",
   
                
 "key":"wx_sell_1"
           
         },
   
                
 {
               
     "type":"click",
       
            
 "name":"分类二",
           
         "key":"wx_sell_2"
   
                
 },
           
         {
       
            
 "type":"click",
           
         "name":"分类三",
   
                
 "key":"wx_sell_3"
           
         }
       
         ]
       
     },
       
     {
           
     "type":"click",
       
         "name":"新闻中心",
   
            
 "key":"wx_news"
           
 }]
         }‘;
   
     self::post($url,$data);
   
 }
    
    /**
    * 
POST 请求指定URL
    *创建一个菜单的时候也可以模拟一次提交
   
 */
    static function post($url,$data) { // 
模拟提交数据函数
        $curl = curl_init(); // 
启动一个CURL会话
        curl_setopt($curl, 
CURLOPT_URL, $url); // 要访问的地址
       
 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); // 
对认证证书来源的检查
        curl_setopt($curl, 
CURLOPT_SSL_VERIFYHOST, FALSE); // 从证书中检查SSL加密算法是否存在
   
     curl_setopt($curl, CURLOPT_USERAGENT, ‘Mozilla/5.0 
(compatible; MSIE 5.01; Windows NT 5.0)‘); // 模拟用户使用的浏览器
   
     curl_setopt($curl, CURLOPT_POST, 1); // 
发送一个常规的Post请求
        curl_setopt($curl, 
CURLOPT_POSTFIELDS, $data); // Post提交的数据包x
       
 curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
   
     curl_setopt($curl, CURLOPT_HEADER, 0); // 
显示返回的Header区域内容
        curl_setopt($curl, 
CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
       
 $tmpInfo = curl_exec($curl); // 执行操作
       
 if (curl_errno($curl)) {
       
     echo ‘Errno‘.curl_error($curl);//捕抓异常
   
     }
       
 curl_close($curl); // 关闭CURL会话
       
 return $tmpInfo; // 返回数据
   
 }</pre>
<p>5.参考的网上的,我直接if判断获取信息</p>
<p></p>
<pre 
name="code" class="php">private function 
receiveEvent($object)
    
{
        $contentStr = 
"";
        switch 
($object->Event)
        
{
            case 
"subscribe":
                
$contentStr[] = array("Title" =>"欢迎关注方倍工作室", "Description" 
=>"点击图片关注或者搜索号码beancube", "PicUrl" 
=>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url" 
=>"weixin://addfriend/beancube");
            
case 
"unsubscribe":
                
$contentStr = 
"";
                
break;
            
case 
"CLICK":
                
switch 
($object->EventKey)
                
{
                    
case 
"company":
                        
$contentStr[] = array("Title" =>"公司简介", "Description" 
=>"方倍工作室提供移动互联网相关的产品及服务,包括新浪微博应用、微信公众平台接口、手机版网站等", "PicUrl" 
=>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url" 
=>"weixin://addfriend/beancube");
                        
break;
                    
default:
                        
$contentStr[] = array("Title" =>"默认菜单回复", "Description" 
=>"您正在使用的是方倍工作室的自定义菜单测试接口", "PicUrl" 
=>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url" 
=>"weixin://addfriend/beancube");
                        
break;
                
}
                
break;
            
default:
                
$contentStr = "receive a new event: 
".$object->Event;
                
break;     
 
       
}
        return 
$contentStr;
    }</pre><br>
注意,,其中的菜单数量就是3个, 
多了都会出现错误
<pre></pre>
</pre></pre>
今天在做自定义菜单的时候因为某个的生成得后台动态调用,可是开始不知道还有上限,开始就是各种测,先获取数组之后再转化成json ,可是还是不对,之后终于找到原因主要是因为有个数限制。
原文:http://www.cnblogs.com/yelp/p/3615595.html