1.依据微信公众平台文档,得知创建自己定义菜单有例如以下规则:
a.眼下自己定义菜单最多包括3个一级菜单,每一个一级菜单最多包括5个二级菜单。一级菜单最多4个汉字,二级菜单最多7个汉字。多出来的部分将会以“...”取代。请注意。创建自己定义菜单后,因为微信client缓存,须要24小时微信client才会展现出来。建议測试时能够尝试取消关注公众账号后再次关注,则能够看到创建后的效果。
注:所以最多仅仅能创建15个功能菜单,多了就出错了!
b.
眼下自己定义菜单接口可实现两种类型button,例如以下:
click: 用户点击click类型button后,微信server会通过消息接口推送消息类型为event 的结构给开发人员(參考消息接口指南),而且带上button中开发人员填写的key值,开发人员能够通过自己定义的key值与用户进行交互; view: 用户点击view类型button后,微信client将会打开开发人员在button中填写的url值 (即网页链接)。达到打开网页的目的,建议与网页授权获取用户基本信息接口结合,获得用户的登入个人信息。
c.
接口调用请求说明
http请求方式:POST(请使用https协议) https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN
2.依据文档提供的规则和接口调用说明,封装成一个类编写代码例如以下
class WeChatCreateMenu { <span style="white-space:pre"> </span>//推断token是否过期,假设过期又一次获取,不然直接从数据库取 public function Get_access_token($appid,$secret){ $db = DBManage::getInstance(); $db->ConnDB(); $sql = "select * from t_token"; if($db-> GetEffectRows($sql) > 0) { $sql = "select * from t_token where TokenFlag='sd'"; $res = $db->GetData($sql); while( $obj = mysqli_fetch_object($res)) { if($obj->TokenTime-60 > time()) //Token还没过期 { return $obj->Token; } else //过期 { break; } } } $url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret; $json=self::DoCurlGetRequest($url); $retData=json_decode($json,true); if(!$retData) { return false; } $token = $retData['access_token']; $expire = $retData['expires_in']; $oldTime = time() + $expire; $sql = "select * from t_token"; if($db-> GetEffectRows($sql) == 0) //无数据 { $sql = "insert into t_token values('sd','$token','$oldTime')"; $db->ExecSql($sql); } else //更新 数据 { $sql = "update t_token set Token='$token',TokenTime='$oldTime' where TokenFlag='sd'"; $db->ExecSql($sql); } //file_put_contents("debug.txt", "token====".$token.PHP_EOL,FILE_APPEND); return $token; } //由于url是https 全部请求不能用file_get_contents,用curl请求json 数据 private function DoCurlGetRequest($url){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$url); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $result = curl_exec($ch); curl_close($ch); return $result; } function DoCurlPostRequest($url, $jsonData){ $con = curl_init((string)$url); curl_setopt($con,CURLOPT_HEADER, false); curl_setopt($con, CURLOPT_POSTFIELDS,$jsonData); curl_setopt($con, CURLOPT_POST, true); curl_setopt($con, CURLOPT_RETURNTRANSFER, true); curl_setopt($con,CURLOPT_SSL_VERIFYPEER,false); //略过证书验证 $result = curl_exec($con) ; if(curl_errno($con)) { file_put_contents("tmp.txt", curl_errno($con).PHP_EOL,FILE_APPEND); } return $result; } public function CreateMenu() { $token = $this->Get_access_token("wxa5fd3441ffa34","a49921c7d71b57850c57d1f589"); $url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$token; //自己定义菜单接口 $menuData = array( 'button' => array( array( 'name' => '走进窗骑', 'sub_button' => array( array( 'type' =>'click', 'name' => '微官网', 'key' => 'sdwgw' ), array( 'type' =>'view', 'name' => '全品牌推广', 'url' => 'http://128.88.44.11/WXProject/DataHtml/html/qpptg.html' ), array( 'type' =>'view', 'name' => '3D影视包装', 'url' => 'http://128.88,44,11/WXProject/DataHtml/html/3d.html' ), array( 'type' =>'view', 'name' => '互动体验', 'url' => 'http://128.88.44.11/WXProject/DataHtml/html/hudong.html' ), array( 'type' =>'view', 'name' => '合作客户', 'url' => 'http://128.88.44.11/WXProject/DataHtml/html/partner.html' ) ) ), array( 'name' => '微楼书', 'sub_button' =>array( array( 'type' =>'click', 'name' => '项目概况', 'key' => 'xmgk' ), array( 'type' =>'view', 'name' => '区位配套', 'url' => 'http://128.88.44.11/WXProject/DataHtml/qwpt.html' ), array( 'type' =>'view', 'name' => '景观鉴赏', 'url' => 'http://128.88.44.11/WXProject/WXPicBrowser/jgjd.php' ), array( 'type' =>'view', 'name' => '户型展示', 'url' => 'http://128.88.44.11/WXProject/WXPicBrowser/jdhx.php' ), array( 'type' =>'view', 'name' => '3D视频', 'url' => 'http://128.88.44.11/WXProject/DataHtml/video.html' ) ) ), array( 'name' => "互动体验", 'sub_button' => array( array( 'type' =>'click', 'name' => '会员中心', 'key' => 'hyzx' ), array( 'type' =>'view', 'name' => '看房预约', 'url' => 'http://128.88.44.11/WXProject/DataPHP/seehouse.php' ), array( 'type' =>'view', 'name' => '智能问答', 'url' => 'http://128.88.44.11/WXProject/DataPHP/test.php' ), array( 'type' =>'view', 'name' => '一键功能', 'url' => 'http://128.88.44.11/WXProject/onetouchdial.html#mp.weixin.qq.com' ), array( 'type' =>'click', 'name' => '刮刮乐', 'key' => 'ggl' ) ) ) ) ); $data = JSON($menuData); $this->DoCurlPostRequest($url,$data); //以https发送post请求 } }写完上面的类,直接在index.php,调用即可了;然后按前面几篇介绍的方法传到sae上面,执行測试
3.大概思路:依据微信的文档说明,首先按规则创建好菜单。然后按规定写好创建自己定义菜单接口。接着用https发送post请求,微信server收到请求。推断发送过来的接口没错。微信server发送创建好的菜单到每一位微信用户的client显示。
----------------------------------------------------------------------------------------------------------------------------------------------------------
有同学说没有数据库操作类,我也直接贴出来吧!
<?php class DBManage { private $conn; private static $_instance; public function __clone() { trigger_error("Clone is not allow!",E_USER_ERROR); } public static function getInstance() { if(!self::$_instance instanceof self) { self::$_instance = new self; } return self::$_instance; } function ConnDB() { $this->conn = mysqli_init(); $ret = mysqli_real_connect($this->conn, "122.128.111.111", "root", "mima222", "wxdb", 6033, "t_userinfo", MYSQLI_CLIENT_FOUND_ROWS); } function ExecSql($sql) { $ret = mysqli_query($this->conn, $sql); return $ret; } function GetEffectRows($sql) { $ret = mysqli_query($this->conn, $sql); return mysqli_num_rows($ret); } function GetData($sql) { $ret = mysqli_query($this->conn, $sql); return $ret; } } ?>