微信通过数据库创建菜单
程序员文章站
2022-04-06 10:19:20
...
1. [文件] menuCreate.php
<?php /** * 微信公众平台-自定义菜单功能开发源代码 * ================================ * Copy 2013-2014 David Tang * http://www.php.cn/ * ================================ * 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 "菜单创建失败"; } */ ?>
上一篇: PHP安全-后门URL
下一篇: 远程用户连接mysql授权_MySQL