Php WeChat development custom menu, php development custom

Source: Internet
Author: User

Php development custom menu, php development custom

Currently, a service number custom menu can contain up to three level-1 menus, and each level-1 menu can contain up to five level-2 menus. A level-1 menu can contain up to four Chinese characters, and a level-2 menu can contain up to seven Chinese characters. The extra content will be replaced. Note that it takes 24 hours for the client to display the custom menu after it is created due to the client cache. We recommend that you try to remove the public account and follow it again during the test to see the effect after creation.

Currently, the custom menu interface supports two types of buttons:
Click:
After you click the click type button, the server pushes the structure of the Message Type event to the developer (reference message Interface Guide) through the message interface, and carries the key value filled by the developer in the button, developers can interact with users through custom key values;
View:
After you click the view type button, the client will open the url value (Web Page Link) filled in by the developer in the button to open the web page, we recommend that you use the webpage authorization interface to obtain the user's personal login information.

API call request description
Http Request Method: POST (Please use https protocol) https://api.weixin.qq.com/cgi-bin/menu/create? Access_token = ACCESS_TOKEN
Request example (use UTF-8 encoding for JSON data)

{"Button": [{"type": "click", "name": "My information", "sub_button": [{"type": "click ", "name": "Thumb query", "key": "BUTTON_1" },{ "type": "click", "name": "Thumb leave", "key ": "BUTTON_2" },{ "type": "view", "name": "id binding", "url": "http: // XXXXXXXXXXXXXXXXX"}]}, {"type": "click", "name": "business process", "key": "BUTTON_3" },{ "name": "employee suggestion ", "sub_button": [{"type": "view", "name": "ideological spark", "url": "http: // XXXXXXXXXXXXXXXXXX "}, {"type": "view", "name": "prize Redemption", "url": "http: // xxxxxxxxxxxxxxxx" },{ "type": "click ", "name": "like us", "key": "BUTTON_ZAN"}]}

Parameter description

Returned results
The returned JSON data packet is as follows:
{"Errcode": 0, "errmsg": "OK "}
When an error occurs, the returned JSON data packet is as follows (in this example, the menu name length is invalid ):
{& Quot; errcode & quot;: 40018, & quot; errmsg & quot;: & quot; invalid button name size & quot "}

The following is the sample code (PHP ).

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

Menu. json

{"Button": [{"type": "click", "name": "My information", "sub_button": [{"type": "click ", "name": "Thumb query", "key": "BUTTON_1" },{ "type": "click", "name": "Thumb leave", "key ": "BUTTON_2" },{ "type": "view", "name": "id binding", "url": "http: // XXXXXXXXXXXXXXXXX"}]}, {"type": "click", "name": "business process", "key": "BUTTON_3" },{ "name": "employee suggestion ", "sub_button": [{"type": "view", "name": "ideological spark", "url": "http: // XXXXXXXXXXXXXXXXXX "}, {"type": "view", "name": "prize Redemption", "url": "http: // xxxxxxxxxxxxxxxx" },{ "type": "click ", "name": "like us", "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, $ ti Mestamp, $ 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 "";} $ resul T = 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 = simp Lexml_load_string ($ postStr, 'simplexmlelement', LIBXML_NOCDATA); $ fromUsername = $ postObj-> FromUserName; $ toUsername = $ postObj-> ToUserName; $ time = time (); // text message template $ 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": // text message $ keyword = trim ($ postObj-> Content); if (! Empty ($ keyword) {$ msgType = "text"; $ contentStr = "$ fromUsername, you sent the text message: $ 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 ": // image message $ msgType =" text "; $ contentStr =" $ fromUsername, you have sent the image information "; $ resu LtStr = sprintf ($ textTpl, $ fromUsername, $ toUsername, $ time, $ msgType, $ contentStr); break; case "voice ": // sound message $ msgType = "text"; $ contentStr = "$ fromUsername, you sent the sound information"; $ resultStr = sprintf ($ textTpl, $ fromUsername, $ toUsername, $ time, $ msgType, $ contentStr); break; case "video": // video message $ msgType = "text"; $ contentStr = "$ fromUsername, you have sent the video information "; $ resultStr = sprintf ($ textTpl, $ fromUsername, $ Username, $ time, $ msgType, $ contentStr); break; case "location": // location message $ msgType = "text"; $ contentStr = "$ fromUsername, you sent the location information "; $ resultStr = sprintf ($ textTpl, $ fromUsername, $ toUsername, $ time, $ msgType, $ contentStr); break; case" link ": // link message $ msgType = "text"; $ contentStr = "$ fromUsername, you sent the link information"; $ resultStr = sprintf ($ textTpl, $ fromUsername, $ toUsername, $ time, $ msgType, $ contentStr); B Reak; case "event": // Event switch (strtolower (trim ($ postObj-> event) {case "subscribe ": // follow events $ msgType = "text"; $ contentStr = "Welcome to XXXXXXX"; $ resultStr = sprintf ($ textTpl, $ fromUsername, $ toUsername, $ time, $ msgType, $ contentStr); break; case "unsubscribe": // cancel follow event break; case "scan ": // QR code scanning event $ msgType = "text"; $ contentStr = "$ fromUsername, you have scanned the QR code"; $ resultStr = sprintf ($ textTpl, $ fro MUsername, $ toUsername, $ time, $ msgType, $ contentStr); break; case "location": // upload a location event $ msgType = "text "; $ contentStr = "$ fromUsername, your upload location"; $ resultStr = sprintf ($ textTpl, $ fromUsername, $ toUsername, $ time, $ msgType, $ contentStr); break; case "click": // custom menu event $ msgType = "text"; $ contentStr = "$ fromUsername, you have clicked the custom menu $ postObj-> EventKey "; if ("BUTTON_ZAN" ==$ postObj-> EventKey) {$ conten TStr = "Thank you for your praise. We will continue to provide better services. ";}$ ResultStr = sprintf ($ textTpl, $ fromUsername, $ toUsername, $ time, $ msgType, $ contentStr); break; default: $ resultStr = "";} break; default: $ resultStr = "";} echo $ resultStr ;}}?>

Custom menu Query

After creating a custom menu using the interface, developers can also use the interface to query the structure of the custom menu.

Request description
Http Request Method: GET
Https://api.weixin.qq.com/cgi-bin/menu/get? Access_token = ACCESS_TOKEN

Return description
Corresponding to the created interface, the correct Json return results: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": "id binding ", "url": "http: \/XXXXXXXX", "sub_button": []}] },{ "type": "click", "name ": "Business Process", "key": "BUTTON_3", "sub_button": []}, {"name": "employee suggestion", "sub_button ": [{"type": "view", "name": "ideological 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 ": []}

Delete custom menu 

After creating a custom menu using the interface, the developer 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
Corresponding to the created interface, the correct Json return results:
{"Errcode": 0, "errmsg": "OK "}

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.