小編給大家分享一下php如何實(shí)現(xiàn)微信開發(fā)自定義菜單,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
成都網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)的開發(fā),更需要了解用戶,從用戶角度來(lái)建設(shè)網(wǎng)站,獲得較好的用戶體驗(yàn)。創(chuàng)新互聯(lián)多年互聯(lián)網(wǎng)經(jīng)驗(yàn),見的多,溝通容易、能幫助客戶提出的運(yùn)營(yíng)建議。作為成都一家網(wǎng)絡(luò)公司,打造的就是網(wǎng)站建設(shè)產(chǎn)品直銷的概念。選擇創(chuàng)新互聯(lián),不只是建站,我們把建站作為產(chǎn)品,不斷的更新、完善,讓每位來(lái)訪用戶感受到浩方產(chǎn)品的價(jià)值服務(wù)。目前微信服務(wù)號(hào)自定義菜單最多包括3個(gè)一級(jí)菜單,每個(gè)一級(jí)菜單最多包含5個(gè)二級(jí)菜單。一級(jí)菜單最多4個(gè)漢字,二級(jí)菜單最多7個(gè)漢字,多出來(lái)的部分將會(huì)以“...”代替。請(qǐng)注意,創(chuàng)建自定義菜單后,由于微信客戶端緩存,需要24小時(shí)微信客戶端才會(huì)展現(xiàn)出來(lái)。建議測(cè)試時(shí)可以嘗試取消關(guān)注公眾賬號(hào)后再次關(guān)注,則可以看到創(chuàng)建后的效果。
目前自定義菜單接口可實(shí)現(xiàn)兩種類型按鈕,如下:
click:
用戶點(diǎn)擊click類型按鈕后,微信服務(wù)器會(huì)通過(guò)消息接口推送消息類型為event 的結(jié)構(gòu)給開發(fā)者(參考消息接口指南),并且?guī)习粹o中開發(fā)者填寫的key值,開發(fā)者可以通過(guò)自定義的key值與用戶進(jìn)行交互;
view:
用戶點(diǎn)擊view類型按鈕后,微信客戶端將會(huì)打開開發(fā)者在按鈕中填寫的url值 (即網(wǎng)頁(yè)鏈接),達(dá)到打開網(wǎng)頁(yè)的目的,建議與網(wǎng)頁(yè)授權(quán)獲取用戶基本信息接口結(jié)合,獲得用戶的登入個(gè)人信息。
接口調(diào)用請(qǐng)求說(shuō)明
http請(qǐng)求方式:POST(請(qǐng)使用https協(xié)議) https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN
請(qǐng)求示例(JSON數(shù)據(jù)請(qǐng)使用UTF-8編碼)
{ "button":[ {"type":"click","name":"我的信息","sub_button":[ {"type":"click","name":"拇指查詢","key":"BUTTON_1"}, {"type":"click","name":"拇指請(qǐng)假","key":"BUTTON_2"}, {"type":"view","name":"工號(hào)綁定","url":"http://XXXXXXXXXXXXXXXXX"}] }, {"type":"click","name":"業(yè)務(wù)流程","key":"BUTTON_3"}, {"name":"員工建議","sub_button":[ {"type":"view","name":"思想火花","url":"http://XXXXXXXXXXXXXXXXXX"}, {"type":"view","name":"獎(jiǎng)品兌換","url":"http://XXXXXXXXXXXXXXXXXX"}, {"type":"click","name":"贊一下我們","key":"BUTTON_ZAN"}] } ] }
參數(shù)說(shuō)明
返回結(jié)果
正確時(shí)的返回JSON數(shù)據(jù)包如下:
{"errcode":0,"errmsg":"ok"}
錯(cuò)誤時(shí)的返回JSON數(shù)據(jù)包如下(示例為無(wú)效菜單名長(zhǎng)度):
{"errcode":40018,"errmsg":"invalid button name size"}
以下是示例代碼(PHP)。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="zh-CN"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <meta name="author" content="Chris Mao" /> </head> <body> <?php $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=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)) { var_dump($output); exit; } $result = json_decode($output); $token = $result->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); ?> </body> </html>
menu.json
{ "button":[ {"type":"click","name":"我的信息","sub_button":[ {"type":"click","name":"拇指查詢","key":"BUTTON_1"}, {"type":"click","name":"拇指請(qǐng)假","key":"BUTTON_2"}, {"type":"view","name":"工號(hào)綁定","url":"http://XXXXXXXXXXXXXXXXX"}] }, {"type":"click","name":"業(yè)務(wù)流程","key":"BUTTON_3"}, {"name":"員工建議","sub_button":[ {"type":"view","name":"思想火花","url":"http://XXXXXXXXXXXXXXXXXX"}, {"type":"view","name":"獎(jiǎng)品兌換","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 = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <Content><![CDATA[%s]]></Content> <FuncFlag>0</FuncFlag> </xml>"; 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 = "感謝您的贊,我們會(huì)繼續(xù)提供更優(yōu)質(zhì)的服務(wù)。"; } $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); ; break; default: $resultStr = ""; } break; default: $resultStr = ""; } echo $resultStr; } } ?>
自定義菜單查詢
使用接口創(chuàng)建自定義菜單后,開發(fā)者還可使用接口查詢自定義菜單的結(jié)構(gòu)。
請(qǐng)求說(shuō)明
http請(qǐng)求方式:GET
https://api.weixin.qq.com/cgi-bin/menu/get?access_token=ACCESS_TOKEN
返回說(shuō)明
對(duì)應(yīng)創(chuàng)建接口,正確的Json返回結(jié)果:
{"menu":{"button":[{"name":"我的信息","sub_button":[{"type":"click","name":"拇指查詢","key":"BUTTON_1","sub_button":[]},{"type":"click","name":"拇指請(qǐng)假","key":"BUTTON_2","sub_button":[]},{"type":"view","name":"工號(hào)綁定","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":"獎(jiǎng)品兌換","url":"http:\/\/XXXXXXXX","sub_button":[]},{"type":"click","name":"贊一下我們","key":"BUTTON_ZAN","sub_button":[]}]}]}}
自定義菜單刪除
使用接口創(chuàng)建自定義菜單后,開發(fā)者還可使用接口刪除當(dāng)前使用的自定義菜單。
請(qǐng)求說(shuō)明
http請(qǐng)求方式:GET
https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=ACCESS_TOKEN
返回說(shuō)明
對(duì)應(yīng)創(chuàng)建接口,正確的Json返回結(jié)果:
{"errcode":0,"errmsg":"ok"}
以上是“php如何實(shí)現(xiàn)微信開發(fā)自定義菜單”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
名稱欄目:php如何實(shí)現(xiàn)微信開發(fā)自定義菜單-創(chuàng)新互聯(lián)
URL標(biāo)題:http://jinyejixie.com/article12/djeidc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供軟件開發(fā)、ChatGPT、小程序開發(fā)、網(wǎng)站排名、標(biāo)簽優(yōu)化、手機(jī)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容