小編給大家分享一下php如何實(shí)現(xiàn)微信開發(fā)自定義菜單,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
成都網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)的開發(fā),更需要了解用戶,從用戶角度來建設(shè)網(wǎng)站,獲得較好的用戶體驗(yàn)。創(chuàng)新互聯(lián)多年互聯(lián)網(wǎng)經(jīng)驗(yàn),見的多,溝通容易、能幫助客戶提出的運(yùn)營建議。作為成都一家網(wǎng)絡(luò)公司,打造的就是網(wǎng)站建設(shè)產(chǎn)品直銷的概念。選擇創(chuàng)新互聯(lián),不只是建站,我們把建站作為產(chǎn)品,不斷的更新、完善,讓每位來訪用戶感受到浩方產(chǎn)品的價(jià)值服務(wù)。目前微信服務(wù)號自定義菜單最多包括3個一級菜單,每個一級菜單最多包含5個二級菜單。一級菜單最多4個漢字,二級菜單最多7個漢字,多出來的部分將會以“...”代替。請注意,創(chuàng)建自定義菜單后,由于微信客戶端緩存,需要24小時(shí)微信客戶端才會展現(xiàn)出來。建議測試時(shí)可以嘗試取消關(guān)注公眾賬號后再次關(guān)注,則可以看到創(chuàng)建后的效果。
目前自定義菜單接口可實(shí)現(xiàn)兩種類型按鈕,如下:
click:
用戶點(diǎn)擊click類型按鈕后,微信服務(wù)器會通過消息接口推送消息類型為event 的結(jié)構(gòu)給開發(fā)者(參考消息接口指南),并且?guī)习粹o中開發(fā)者填寫的key值,開發(fā)者可以通過自定義的key值與用戶進(jìn)行交互;
view:
用戶點(diǎn)擊view類型按鈕后,微信客戶端將會打開開發(fā)者在按鈕中填寫的url值 (即網(wǎng)頁鏈接),達(dá)到打開網(wǎng)頁的目的,建議與網(wǎng)頁授權(quán)獲取用戶基本信息接口結(jié)合,獲得用戶的登入個人信息。
接口調(diào)用請求說明
http請求方式:POST(請使用https協(xié)議) https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN
請求示例(JSON數(shù)據(jù)請使用UTF-8編碼)
{ "button":[ {"type":"click","name":"我的信息","sub_button":[ {"type":"click","name":"拇指查詢","key":"BUTTON_1"}, {"type":"click","name":"拇指請假","key":"BUTTON_2"}, {"type":"view","name":"工號綁定","url":"http://XXXXXXXXXXXXXXXXX"}] }, {"type":"click","name":"業(yè)務(wù)流程","key":"BUTTON_3"}, {"name":"員工建議","sub_button":[ {"type":"view","name":"思想火花","url":"http://XXXXXXXXXXXXXXXXXX"}, {"type":"view","name":"獎品兌換","url":"http://XXXXXXXXXXXXXXXXXX"}, {"type":"click","name":"贊一下我們","key":"BUTTON_ZAN"}] } ] }
參數(shù)說明
返回結(jié)果
正確時(shí)的返回JSON數(shù)據(jù)包如下:
{"errcode":0,"errmsg":"ok"}
錯誤時(shí)的返回JSON數(shù)據(jù)包如下(示例為無效菜單名長度):
{"errcode":40018,"errmsg":"invalid button name size"}
以下是示例代碼(PHP)。
access_token; //創(chuàng)建菜單 $url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=$token"; $jsonData = file_get_contents("menu.json"); $ch = curl_init($url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData); curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); $output = curl_exec($ch); curl_close($ch); var_dump($output); ?>
menu.json
{ "button":[ {"type":"click","name":"我的信息","sub_button":[ {"type":"click","name":"拇指查詢","key":"BUTTON_1"}, {"type":"click","name":"拇指請假","key":"BUTTON_2"}, {"type":"view","name":"工號綁定","url":"http://XXXXXXXXXXXXXXXXX"}] }, {"type":"click","name":"業(yè)務(wù)流程","key":"BUTTON_3"}, {"name":"員工建議","sub_button":[ {"type":"view","name":"思想火花","url":"http://XXXXXXXXXXXXXXXXXX"}, {"type":"view","name":"獎品兌換","url":"http://XXXXXXXXXXXXXXXXXX"}, {"type":"click","name":"贊一下我們","key":"BUTTON_ZAN"}] } ] }
響應(yīng)自定義菜單事件
$wechatObj = new wechatCallbackAPI(); if (isset($_GET["echostr"])) { $wechatObj->valid(); } else { $wechatObj->responseMsg(); } class wechatCallbackAPI { private $token = "WEIXIN"; private $appId = "APPID"; private $appSecret = "APPSECRET"; private function checkSignature() { $signature = $_GET["signature"]; $timestamp = $_GET["timestamp"]; $nonce = $_GET["nonce"]; $tmpArr = array($this->token, $timestamp, $nonce); sort($tmpArr); $tmpStr = implode($tmpArr); $tmpStr = sha1($tmpStr); if($tmpStr == $signature) { return true; } else { return false; } } private function getAccessToken() { $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appId&secret=$this->appSecret"; $ch = curl_init($url); $curl_setopt($ch, CURLOPT_HEADER, 0); $curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $curl_setopt($ch, CURLOPT_POST, 0); $curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); $curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); $output = curl_exec($ch); curl_close($ch); if (empty($output)) { return ""; } $result = json_decode($result); return $result->access_token; } public function valid() { $echoStr = $_GET["echostr"]; //valid signature, option if($this->checkSignature()){ echo $echoStr; exit; } } public function responseMsg() { //get post data, May be due to the different environments $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; if (empty($postStr)){ echo ""; exit; } //extract post data $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $fromUsername = $postObj->FromUserName; $toUsername = $postObj->ToUserName; $time = time(); //文本消息模板 $textTpl = ""; switch (strtolower(trim($postObj->MsgType))) { case "text": //文本消息 $keyword = trim($postObj->Content); if(!empty($keyword)) { $msgType = "text"; $contentStr = "$fromUsername, 您發(fā)送了文本信息: $keyword "; if (strtolower($keyword) == "time") { $contentStr = date("Y-m-d H:i:s", $time); } $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); } else { $resultStr = "Input something..."; } break; case "image": //圖片消息 $msgType = "text"; $contentStr = "$fromUsername, 您發(fā)送了圖片信息"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); break; case "voice": //聲音消息 $msgType = "text"; $contentStr = "$fromUsername, 您發(fā)送了聲音信息"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); break; case "video": //視頻消息 $msgType = "text"; $contentStr = "$fromUsername, 您發(fā)送了視頻信息"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); break; case "location": //位置消息 $msgType = "text"; $contentStr = "$fromUsername, 您發(fā)送了位置信息"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); break; case "link": //鏈接消息 $msgType = "text"; $contentStr = "$fromUsername, 您發(fā)送了鏈接信息"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); break; case "event": //事件 switch (strtolower(trim($postObj->Event))) { case "subscribe": //關(guān)注事件 $msgType = "text"; $contentStr = "歡迎您關(guān)注XXXXXXX"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); break; case "unsubscribe": //取消關(guān)注事件 break; case "scan": //用戶已關(guān)注時(shí)掃描二維碼事件 $msgType = "text"; $contentStr = "$fromUsername, 您掃描了二維碼"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); break; case "location": //上傳地理位置事件 $msgType = "text"; $contentStr = "$fromUsername, 您上傳地理位置"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); break; case "click": //自定義菜單事件 $msgType = "text"; $contentStr = "$fromUsername, 您點(diǎn)擊了自定義菜單 $postObj->EventKey "; if ("BUTTON_ZAN" == $postObj->EventKey) { $contentStr = "感謝您的贊,我們會繼續(xù)提供更優(yōu)質(zhì)的服務(wù)。"; } $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); ; break; default: $resultStr = ""; } break; default: $resultStr = ""; } echo $resultStr; } } ?> %s 0
自定義菜單查詢
使用接口創(chuàng)建自定義菜單后,開發(fā)者還可使用接口查詢自定義菜單的結(jié)構(gòu)。
請求說明
http請求方式:GET
https://api.weixin.qq.com/cgi-bin/menu/get?access_token=ACCESS_TOKEN
返回說明
對應(yīng)創(chuàng)建接口,正確的Json返回結(jié)果:
{"menu":{"button":[{"name":"我的信息","sub_button":[{"type":"click","name":"拇指查詢","key":"BUTTON_1","sub_button":[]},{"type":"click","name":"拇指請假","key":"BUTTON_2","sub_button":[]},{"type":"view","name":"工號綁定","url":"http:\/\/XXXXXXXX","sub_button":[]}]},{"type":"click","name":"業(yè)務(wù)流程","key":"BUTTON_3","sub_button":[]},{"name":"員工建議","sub_button":[{"type":"view","name":"思想火花","url":"http:\/\/XXXXXXXX","sub_button":[]},{"type":"view","name":"獎品兌換","url":"http:\/\/XXXXXXXX","sub_button":[]},{"type":"click","name":"贊一下我們","key":"BUTTON_ZAN","sub_button":[]}]}]}}
自定義菜單刪除
使用接口創(chuàng)建自定義菜單后,開發(fā)者還可使用接口刪除當(dāng)前使用的自定義菜單。
請求說明
http請求方式:GET
https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=ACCESS_TOKEN
返回說明
對應(yīng)創(chuàng)建接口,正確的Json返回結(jié)果:
{"errcode":0,"errmsg":"ok"}
以上是“php如何實(shí)現(xiàn)微信開發(fā)自定義菜單”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!