首页 >php教程 >PHP源码 >微信通过数据库创建菜单

微信通过数据库创建菜单

PHP中文网
PHP中文网原创
2016-05-25 16:58:08962浏览

1. [文件]     menuCreate.php 

<?php
/**
 * 微信公众平台-自定义菜单功能开发源代码
 * ================================
 * Copy 2013-2014 David Tang
 * http://www.php.cn/
 * ================================
 * Author:
 * 个人微信:mchina_tang
 * 公众微信:博士伦订阅号菜单
 * Date:2013-08-25
 */
header(&#39;Content-Type: text/html; charset=UTF-8&#39;);

$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[&#39;access_token&#39;];

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=&#39;{
     "button":[
     {	
          "name":"&#39;.$menuArray[1][0].&#39;",
          "sub_button":[&#39;;
	   if(!empty($menuArray[11][0])){
	   	  if($menuArray[11][1]==&#39;click&#39;){
		   $data .=&#39;
		    {
               "type":"&#39;.$menuArray[11][1].&#39;",
               "name":"&#39;.$menuArray[11][0].&#39;",
               "key":"V1001_Product011"
            },&#39;;
          }else{
             $data .=&#39;
		     {
               "view":"&#39;.$menuArray[11][1].&#39;",
               "name":"&#39;.$menuArray[11][0].&#39;",
               "url":"&#39;.$menuArray[11][2].&#39;"
              },&#39;;
          }
        }
        if(!empty($menuArray[12][0])){	  
        	if($menuArray[12][1]==&#39;click&#39;){
			$data.=&#39;
			{
			   "type":"&#39;.$menuArray[12][1].&#39;",
	           "name":"&#39;.$menuArray[12][0].&#39;",
	           "key":"V1001_Product012"
	         },&#39;;
	        }else{
	       	 $data .=&#39;
		        {
	               "view":"&#39;.$menuArray[12][1].&#39;",
	               "name":"&#39;.$menuArray[12][0].&#39;",
	               "url":"&#39;.$menuArray[12][2].&#39;"
	           	},&#39;;
	         }
         }
         if(!empty($menuArray[13][0])){
         	 if($menuArray[13][1]==&#39;click&#39;){
		      $data .=&#39;
		      	{
	               "type":"&#39;.$menuArray[13][1].&#39;",
	               "name":"&#39;.$menuArray[13][0].&#39;",
	               "key":"V1001_Product013"
	            },&#39;;
              }else{
	              $data .=&#39;
		   		{
	               "view":"&#39;.$menuArray[13][1].&#39;",
	               "name":"&#39;.$menuArray[13][0].&#39;",
	               "url":"&#39;.$menuArray[13][2].&#39;"
	           },&#39;;
              }            
          }
          if(!empty($menuArray[14][0])){
          	 if($menuArray[14][1]==&#39;click&#39;){
	            $data .=&#39;
	         {
	               "type":"&#39;.$menuArray[14][1].&#39;",
	               "name":"&#39;.$menuArray[14][0].&#39;",
	               "key":"V1001_Product014"
	            },&#39;;
	         }else{
	         	$data .=&#39;
		  		{
	               "view":"&#39;.$menuArray[14][1].&#39;",
	               "name":"&#39;.$menuArray[14][0].&#39;",
	               "url":"&#39;.$menuArray[14][2].&#39;"
		         },&#39;;
	         }
          }
          if(!empty($menuArray[15][0])){
          	  if($menuArray[15][1]==&#39;click&#39;){
	          	 $data .=&#39;
	          	 {
	               "type":"&#39;.$menuArray[15][1].&#39;",
	               "name":"&#39;.$menuArray[15][0].&#39;",
	               "key":"V1001_Product015"
	             }&#39;;
              }else{
              	$data .=&#39;
		  		{
	               "view":"&#39;.$menuArray[15][1].&#39;",
	               "name":"&#39;.$menuArray[15][0].&#39;",
	               "url":"&#39;.$menuArray[15][2].&#39;"
		         },&#39;;        
              }    
          }
           $data .=&#39;]
      },
      {
            "name":"&#39;.$menuArray[2][0].&#39;",
            "sub_button":[
        &#39;;
        if(!empty($menuArray[21][0])){
         	if($menuArray[21][1]==&#39;click&#39;){	
	          $data .=&#39;  
	           { 
	           	   "type":"&#39;.$menuArray[21][1].&#39;",
	               "name":"&#39;.$menuArray[21][0].&#39;",
	               "key":"V1001_Personal021"  
	           },&#39;;
	        }else{
	       	   $data .=&#39;
		  		{
	               "view":"&#39;.$menuArray[21][1].&#39;",
	               "name":"&#39;.$menuArray[21][0].&#39;",
	               "url":"&#39;.$menuArray[21][2].&#39;"
		         },&#39;;
	        } 	   
        } 
        if(!empty($menuArray[22][0])){
        	if($menuArray[22][1]==&#39;click&#39;){	
	        	$data .=&#39;
	        	{ 
	               "type":"&#39;.$menuArray[22][1].&#39;",
	               "name":"&#39;.$menuArray[22][0].&#39;",
	               "key":"V1001_Personal022"
	            },&#39;;
           }else{
	       	   $data .=&#39;
		  		{
	               "view":"&#39;.$menuArray[22][1].&#39;",
	               "name":"&#39;.$menuArray[22][0].&#39;",
	               "url":"&#39;.$menuArray[22][2].&#39;"
		         },&#39;;
	        }   
        }
        if(!empty($menuArray[23][0])){
        	if($menuArray[23][1]==&#39;click&#39;){		
	            $data .=&#39;
	        	{
	               "type":"&#39;.$menuArray[23][1].&#39;",
	               "name":"&#39;.$menuArray[23][0].&#39;",
	               "key":"V1001_Personal023"
	            },&#39;;
           }else{
	       	   $data .=&#39;
		  		{
	               "view":"&#39;.$menuArray[23][1].&#39;",
	               "name":"&#39;.$menuArray[23][0].&#39;",
	               "url":"&#39;.$menuArray[23][2].&#39;"
		         },&#39;;
	        }
        }
        if(!empty($menuArray[24][0])){
        	if($menuArray[24][1]==&#39;click&#39;){	
	           $data .=&#39;	     	   
            {
               "type":"&#39;.$menuArray[24][1].&#39;",
               "name":"&#39;.$menuArray[24][0].&#39;",
               "key":"V1001_Personal024"
            },&#39;;
            }else{
	       	   $data .=&#39;
	  		{
               "view":"&#39;.$menuArray[24][1].&#39;",
               "name":"&#39;.$menuArray[24][0].&#39;",
               "url":"&#39;.$menuArray[24][2].&#39;"
	         },&#39;;
	        }
        }
        if(!empty($menuArray[25][0])){
        	if($menuArray[25][1]==&#39;click&#39;){	
	        	$data .=&#39;
            {
               "type":"&#39;.$menuArray[25][1].&#39;",
               "name":"&#39;.$menuArray[25][0].&#39;",
               "key":"V1001_Personal025"
            }&#39;;
            }else{
	       	   $data .=&#39;
	  		{
               "view":"&#39;.$menuArray[25][1].&#39;",
               "name":"&#39;.$menuArray[25][0].&#39;",
               "url":"&#39;.$menuArray[25][2].&#39;"
	        }&#39;;
	        }
        }
        $data .=&#39;]
      },
      {
           "name":"&#39;.$menuArray[3][0].&#39;",
           "sub_button":[&#39;;
         if(!empty($menuArray[31][0])){
         	 if($menuArray[31][1]==&#39;click&#39;){	
	          	$data .=&#39;  	
	            {
	               "type":"&#39;.$menuArray[31][1].&#39;",
	               "name":"&#39;.$menuArray[31][0].&#39;",
	               "key":"V1001_Activity031"
	            },&#39;;
            }else{
	       	   $data .=&#39;
		  		{
	               "view":"&#39;.$menuArray[31][1].&#39;",
	               "name":"&#39;.$menuArray[31][0].&#39;",
	               "url":"&#39;.$menuArray[31][2].&#39;"
		        }&#39;;
	        }
         }
         if(!empty($menuArray[32][0])){
         	 if($menuArray[32][1]==&#39;click&#39;){
	         	$data .=&#39;
	            {
	               "type":"&#39;.$menuArray[32][1].&#39;",
	               "name":"&#39;.$menuArray[32][0].&#39;",
	               "key":"V1001_Activity032"
	            },&#39;;
            }else{
	       	   $data .=&#39;
		  		{
	               "view":"&#39;.$menuArray[32][1].&#39;",
	               "name":"&#39;.$menuArray[32][0].&#39;",
	               "url":"&#39;.$menuArray[32][2].&#39;"
		        }&#39;;
	        }
         }
      if(!empty($menuArray[33][0])){
        if($menuArray[33][1]==&#39;click&#39;){
	       	$data .=&#39;
            {
               "type":"&#39;.$menuArray[33][1].&#39;",
               "name":"&#39;.$menuArray[33][0].&#39;",
               "key":"V1001_Activity033"
            },&#39;;
        }else{
	        $data .=&#39;
	  		{
               "view":"&#39;.$menuArray[33][1].&#39;",
               "name":"&#39;.$menuArray[33][0].&#39;",
               "url":"&#39;.$menuArray[33][2].&#39;"
	        }&#39;;
	    }
       }
      if(!empty($menuArray[34][0])){
      	  if($menuArray[34][1]==&#39;click&#39;){
         	$data .=&#39;
            {
               "type":"&#39;.$menuArray[34][1].&#39;",
               "name":"&#39;.$menuArray[34][0].&#39;",
               "key":"V1001_Activity034"
            },&#39;;
          }else{
	        $data .=&#39;
	  		{
               "view":"&#39;.$menuArray[34][1].&#39;",
               "name":"&#39;.$menuArray[34][0].&#39;",
               "url":"&#39;.$menuArray[34][2].&#39;"
	        }&#39;;
	    }
      }
      if(!empty($menuArray[35][0])){
      	 if($menuArray[35][1]==&#39;click&#39;){
         	$data .=&#39;
            {
               "type":"&#39;.$menuArray[35][1].&#39;",
               "name":"&#39;.$menuArray[35][0].&#39;",
               "key":"V1001_Activity035"
            }&#39;;
          }else{
	        $data .=&#39;
	  		{
               "view":"&#39;.$menuArray[35][1].&#39;",
               "name":"&#39;.$menuArray[35][0].&#39;",
               "url":"&#39;.$menuArray[35][2].&#39;"
	        }&#39;;
	     }
       }
         $data .=&#39;
           ]
       }]
    }&#39;;
 
//echo "<pre class="brush:php;toolbar:false">";
//echo $data;
///echo "
"; $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