• 技术文章 >php教程 >PHP源码

    微信通过数据库创建菜单

    PHP中文网PHP中文网2016-05-25 16:58:08原创376

    1. [文件] menuCreate.php

    <?php
    /**
     * 微信公众平台-自定义菜单功能开发源代码
     * ================================
     * Copy 2013-2014 David Tang
     * //m.sbmmt.com/
     * ================================
     * Author:
     * 个人微信:mchina_tang
     * 公众微信:博士伦订阅号菜单
     * Date:2013-08-25
     */
    header('Content-Type: text/html; charset=UTF-8');
    
    $APPID="######";
    $APPSECRET="######";
      
    /////$TOKEN_URL="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx34c6e7c75afa2749&secret=88388ddeb5b9f523f562cb161699912c";
    $TOKEN_URL="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$APPID."&secret=".$APPSECRET;
    
    $json=file_get_contents($TOKEN_URL);
    $result=json_decode($json,true);
    
    $ACC_TOKEN= $result['access_token'];
    
    require_once("wx-config.php");
    $sql="select MsgType,Content,Location,KeyWords from menumsg order by Location";
    $result = mysql_query($sql,$dbh);
    
    $menuArray = array();
    while($row = mysql_fetch_array($result)){
    	$menuArray[$row["Location"]][0]= $row["KeyWords"];
    	$menuArray[$row["Location"]][1]= $row["MsgType"];
    	$menuArray[$row["Location"]][2]= $row["Content"];
    }
    $data='{
         "button":[
         {	
              "name":"'.$menuArray[1][0].'",
              "sub_button":[';
    	   if(!empty($menuArray[11][0])){
    	   	  if($menuArray[11][1]=='click'){
    		   $data .='
    		    {
                   "type":"'.$menuArray[11][1].'",
                   "name":"'.$menuArray[11][0].'",
                   "key":"V1001_Product011"
                },';
              }else{
                 $data .='
    		     {
                   "view":"'.$menuArray[11][1].'",
                   "name":"'.$menuArray[11][0].'",
                   "url":"'.$menuArray[11][2].'"
                  },';
              }
            }
            if(!empty($menuArray[12][0])){	  
            	if($menuArray[12][1]=='click'){
    			$data.='
    			{
    			   "type":"'.$menuArray[12][1].'",
    	           "name":"'.$menuArray[12][0].'",
    	           "key":"V1001_Product012"
    	         },';
    	        }else{
    	       	 $data .='
    		        {
    	               "view":"'.$menuArray[12][1].'",
    	               "name":"'.$menuArray[12][0].'",
    	               "url":"'.$menuArray[12][2].'"
    	           	},';
    	         }
             }
             if(!empty($menuArray[13][0])){
             	 if($menuArray[13][1]=='click'){
    		      $data .='
    		      	{
    	               "type":"'.$menuArray[13][1].'",
    	               "name":"'.$menuArray[13][0].'",
    	               "key":"V1001_Product013"
    	            },';
                  }else{
    	              $data .='
    		   		{
    	               "view":"'.$menuArray[13][1].'",
    	               "name":"'.$menuArray[13][0].'",
    	               "url":"'.$menuArray[13][2].'"
    	           },';
                  }            
              }
              if(!empty($menuArray[14][0])){
              	 if($menuArray[14][1]=='click'){
    	            $data .='
    	         {
    	               "type":"'.$menuArray[14][1].'",
    	               "name":"'.$menuArray[14][0].'",
    	               "key":"V1001_Product014"
    	            },';
    	         }else{
    	         	$data .='
    		  		{
    	               "view":"'.$menuArray[14][1].'",
    	               "name":"'.$menuArray[14][0].'",
    	               "url":"'.$menuArray[14][2].'"
    		         },';
    	         }
              }
              if(!empty($menuArray[15][0])){
              	  if($menuArray[15][1]=='click'){
    	          	 $data .='
    	          	 {
    	               "type":"'.$menuArray[15][1].'",
    	               "name":"'.$menuArray[15][0].'",
    	               "key":"V1001_Product015"
    	             }';
                  }else{
                  	$data .='
    		  		{
    	               "view":"'.$menuArray[15][1].'",
    	               "name":"'.$menuArray[15][0].'",
    	               "url":"'.$menuArray[15][2].'"
    		         },';        
                  }    
              }
               $data .=']
          },
          {
                "name":"'.$menuArray[2][0].'",
                "sub_button":[
            ';
            if(!empty($menuArray[21][0])){
             	if($menuArray[21][1]=='click'){	
    	          $data .='  
    	           { 
    	           	   "type":"'.$menuArray[21][1].'",
    	               "name":"'.$menuArray[21][0].'",
    	               "key":"V1001_Personal021"  
    	           },';
    	        }else{
    	       	   $data .='
    		  		{
    	               "view":"'.$menuArray[21][1].'",
    	               "name":"'.$menuArray[21][0].'",
    	               "url":"'.$menuArray[21][2].'"
    		         },';
    	        } 	   
            } 
            if(!empty($menuArray[22][0])){
            	if($menuArray[22][1]=='click'){	
    	        	$data .='
    	        	{ 
    	               "type":"'.$menuArray[22][1].'",
    	               "name":"'.$menuArray[22][0].'",
    	               "key":"V1001_Personal022"
    	            },';
               }else{
    	       	   $data .='
    		  		{
    	               "view":"'.$menuArray[22][1].'",
    	               "name":"'.$menuArray[22][0].'",
    	               "url":"'.$menuArray[22][2].'"
    		         },';
    	        }   
            }
            if(!empty($menuArray[23][0])){
            	if($menuArray[23][1]=='click'){		
    	            $data .='
    	        	{
    	               "type":"'.$menuArray[23][1].'",
    	               "name":"'.$menuArray[23][0].'",
    	               "key":"V1001_Personal023"
    	            },';
               }else{
    	       	   $data .='
    		  		{
    	               "view":"'.$menuArray[23][1].'",
    	               "name":"'.$menuArray[23][0].'",
    	               "url":"'.$menuArray[23][2].'"
    		         },';
    	        }
            }
            if(!empty($menuArray[24][0])){
            	if($menuArray[24][1]=='click'){	
    	           $data .='	     	   
                {
                   "type":"'.$menuArray[24][1].'",
                   "name":"'.$menuArray[24][0].'",
                   "key":"V1001_Personal024"
                },';
                }else{
    	       	   $data .='
    	  		{
                   "view":"'.$menuArray[24][1].'",
                   "name":"'.$menuArray[24][0].'",
                   "url":"'.$menuArray[24][2].'"
    	         },';
    	        }
            }
            if(!empty($menuArray[25][0])){
            	if($menuArray[25][1]=='click'){	
    	        	$data .='
                {
                   "type":"'.$menuArray[25][1].'",
                   "name":"'.$menuArray[25][0].'",
                   "key":"V1001_Personal025"
                }';
                }else{
    	       	   $data .='
    	  		{
                   "view":"'.$menuArray[25][1].'",
                   "name":"'.$menuArray[25][0].'",
                   "url":"'.$menuArray[25][2].'"
    	        }';
    	        }
            }
            $data .=']
          },
          {
               "name":"'.$menuArray[3][0].'",
               "sub_button":[';
             if(!empty($menuArray[31][0])){
             	 if($menuArray[31][1]=='click'){	
    	          	$data .='  	
    	            {
    	               "type":"'.$menuArray[31][1].'",
    	               "name":"'.$menuArray[31][0].'",
    	               "key":"V1001_Activity031"
    	            },';
                }else{
    	       	   $data .='
    		  		{
    	               "view":"'.$menuArray[31][1].'",
    	               "name":"'.$menuArray[31][0].'",
    	               "url":"'.$menuArray[31][2].'"
    		        }';
    	        }
             }
             if(!empty($menuArray[32][0])){
             	 if($menuArray[32][1]=='click'){
    	         	$data .='
    	            {
    	               "type":"'.$menuArray[32][1].'",
    	               "name":"'.$menuArray[32][0].'",
    	               "key":"V1001_Activity032"
    	            },';
                }else{
    	       	   $data .='
    		  		{
    	               "view":"'.$menuArray[32][1].'",
    	               "name":"'.$menuArray[32][0].'",
    	               "url":"'.$menuArray[32][2].'"
    		        }';
    	        }
             }
          if(!empty($menuArray[33][0])){
            if($menuArray[33][1]=='click'){
    	       	$data .='
                {
                   "type":"'.$menuArray[33][1].'",
                   "name":"'.$menuArray[33][0].'",
                   "key":"V1001_Activity033"
                },';
            }else{
    	        $data .='
    	  		{
                   "view":"'.$menuArray[33][1].'",
                   "name":"'.$menuArray[33][0].'",
                   "url":"'.$menuArray[33][2].'"
    	        }';
    	    }
           }
          if(!empty($menuArray[34][0])){
          	  if($menuArray[34][1]=='click'){
             	$data .='
                {
                   "type":"'.$menuArray[34][1].'",
                   "name":"'.$menuArray[34][0].'",
                   "key":"V1001_Activity034"
                },';
              }else{
    	        $data .='
    	  		{
                   "view":"'.$menuArray[34][1].'",
                   "name":"'.$menuArray[34][0].'",
                   "url":"'.$menuArray[34][2].'"
    	        }';
    	    }
          }
          if(!empty($menuArray[35][0])){
          	 if($menuArray[35][1]=='click'){
             	$data .='
                {
                   "type":"'.$menuArray[35][1].'",
                   "name":"'.$menuArray[35][0].'",
                   "key":"V1001_Activity035"
                }';
              }else{
    	        $data .='
    	  		{
                   "view":"'.$menuArray[35][1].'",
                   "name":"'.$menuArray[35][0].'",
                   "url":"'.$menuArray[35][2].'"
    	        }';
    	     }
           }
             $data .='
               ]
           }]
        }';
     
    //echo "<pre>";
    //echo $data;
    ///echo "</pre>";
    
    $MENU_URL= "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$ACC_TOKEN;
    
    $ch = curl_init($MENU_URL);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length:'.strlen($data)));
    
    $info = curl_exec($ch);
    //创建成功返回:{"errcode":0,"errmsg":"ok"}
    $menu = json_decode($info);
    
    var_dump($menu);
    /*
    if($menu->errmsg == "ok"){
    	echo "菜单创建成功";
    }else{
    	echo "菜单创建失败";
    }
    */
    ?>
    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    专题推荐:
    上一篇:thinkphp+jcrop打造的强迫症头像制作 下一篇:PHP添加一个按钮的2中方式,单引号和双引号2种写法。
    PHP编程就业班

    相关文章推荐

    • php学习笔记之面向对象编程• Yaf框架入门只hello yaf• php面向对象之工厂模式示例• php 字符串操作函数 (1/2)• php 广告点击统计代码

    全部评论我要评论

  • 取消发布评论发送
  • 1/1

    PHP中文网