Home>Article>Backend Development> Sharing methods for developing custom menus in PHP WeChat
This article mainly introduces the development of custom menus in PHP WeChat in detail, which has certain reference value. Interested friends can refer to it
Currently, the custom menu of WeChat service accounts includes up to 3 Each first-level menu contains up to 5 second-level menus. The first-level menu can contain up to 4 Chinese characters, and the second-level menu can contain up to 7 Chinese characters. The extra parts will be replaced by "...". Please note that after creating a custom menu, it will take 24 hours for the WeChat client to display it due to WeChat client caching. It is recommended that when testing, you can try to unfollow the public account and follow it again, and you can see the effect after creation.
Currently, the custom menu interface can implement two types of buttons, as follows:
click:After the user clicks the click type button, the WeChat server will send the message through the message interface Push the message type event structure to the developer (refer to the Message Interface Guide), and bring the key value filled in by the developer in the button. The developer can interact with the user through the customized key value;
view:
After the user clicks the view type button, the WeChat client will open the url value (i.e. web link) filled in by the developer in the button to achieve the purpose of opening the web page. It is recommended to match the web page Authorize to obtain the user's basic information interface and obtain the user's login personal information.
Interface call request descriptionhttp request method: POST (please use https protocol) https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN
Request example (please use UTF-8 encoding for JSON data)
{ "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":"业务流程","key":"BUTTON_3"}, {"name":"员工建议","sub_button":[ {"type":"view","name":"思想火花","url":"http://XXXXXXXXXXXXXXXXXX"}, {"type":"view","name":"奖品兑换","url":"http://XXXXXXXXXXXXXXXXXX"}, {"type":"click","name":"赞一下我们","key":"BUTTON_ZAN"}] } ] }
Parameter description
Return result
Return JSON data when correct The package is as follows:
{"errcode":0,"errmsg":"ok"}
The returned JSON data package in case of error is as follows (an example is invalid menu name length):
{"errcode":40018 ,"errmsg":"invalid button name size"}
The following is sample code (PHP).
access_token; //创建菜单 $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":"业务流程","key":"BUTTON_3"}, {"name":"员工建议","sub_button":[ {"type":"view","name":"思想火花","url":"http://XXXXXXXXXXXXXXXXXX"}, {"type":"view","name":"奖品兑换","url":"http://XXXXXXXXXXXXXXXXXX"}, {"type":"click","name":"赞一下我们","key":"BUTTON_ZAN"}] } ] }
Respond to custom menu events
$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, 您发送了文本信息: $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, 您发送了图片信息"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); break; case "voice": //声音消息 $msgType = "text"; $contentStr = "$fromUsername, 您发送了声音信息"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); break; case "video": //视频消息 $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 "link": //链接消息 $msgType = "text"; $contentStr = "$fromUsername, 您发送了链接信息"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); break; case "event": //事件 switch (strtolower(trim($postObj->Event))) { case "subscribe": //关注事件 $msgType = "text"; $contentStr = "欢迎您关注XXXXXXX"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); break; case "unsubscribe": //取消关注事件 break; case "scan": //用户已关注时扫描二维码事件 $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, 您点击了自定义菜单 $postObj->EventKey "; if ("BUTTON_ZAN" == $postObj->EventKey) { $contentStr = "感谢您的赞,我们会继续提供更优质的服务。"; } $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); ; break; default: $resultStr = ""; } break; default: $resultStr = ""; } echo $resultStr; } } ?> %s 0
Custom menu query
Use the interface to create custom After creating the menu, developers can also use the interface to query the structure of the custom menu.
Request instructions
http request method: GET
https://api.weixin.qq.com/cgi-bin/menu/get?access_token=ACCESS_TOKEN
Return instructions
Corresponding to the creation interface, the correct Json return result is:
Copy codeThe code is as follows:
{"menu":{"button":[{"name":"My information","sub_button":[ {"type":"click","name":"Thumb query","key":"BUTTON_1","sub_button":[]},{"type":"click","name":"Thumb leave ","key":"BUTTON_2","sub_button":[]},{"type":"view","name":"Work ID Binding","url":"http:\/\/XXXXXXXX ","sub_button":[]}]},{"type":"click","name":"Business Process","key":"BUTTON_3","sub_button":[]},{"name" :"Employee Suggestions","sub_button":[{"type":"view","name":"Idea Spark","url":"http:\/\/XXXXXXXX","sub_button":[]} ,{"type":"view","name":"Prize Redemption","url":"http:\/\/XXXXXXXX","sub_button":[]},{"type":"click", "name":"Like us","key":"BUTTON_ZAN","sub_button":[]}]}]}}
Custom menu deletion
After using the interface to create a custom menu, developers can also use the interface to delete the currently used custom menu.
Request description
http request method: GET
https://api.weixin.qq.com/cgi-bin/menu/delete ?access_token=ACCESS_TOKEN
Return description
Corresponds to the creation interface, the correct Json return result:
{"errcode":0,"errmsg" :"ok"}
Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.
Related recommendations:
PHP array addition operation and the difference from array_merge
##Usage of call_user_func_array callback function in PHP
Notes on how PHP uses the curl function to send Post requests
The above is the detailed content of Sharing methods for developing custom menus in PHP WeChat. For more information, please follow other related articles on the PHP Chinese website!