PHP WeChat develop a custom menu method sharing

Source: Internet
Author: User
This article mainly for you to introduce the PHP development of custom menus, with a certain reference value, interested in small partners can refer to

Currently the service number customization menu includes up to 3 level menus, each with a maximum of 5 level two menus. A menu of up to 4 kanji, a two-level menu with a maximum of 7 Chinese characters, and the extra part will be replaced with "...". Note that after you create a custom menu, it takes 24 hours for clients to show up due to client-side caching. When you try to cancel your attention to the public account after testing, you can see the effect after the creation.

The current custom menu interface implements two types of buttons, as follows:
Click : After the user clicks the Click type button, the server pushes the message type to the developer via the message interface (refer to the Message Interface Guide), and with the key value entered by the developer in the button, the developer can interact with the user via a custom key value ;
View
After the user clicks on the View Type button, the client will open the URL value (ie link) that the developer fills in the button, which is the purpose of opening the webpage, and it is suggested to combine with the user's basic information interface to obtain the user's login personal information.

Interface Call Request Description HTTP request mode: POST (use HTTPS protocol) Https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN
Request example (JSON data using UTF-8 encoding)

{"button": [  {"Type": "click", "Name": "My Message", "Sub_button": [   {"Type": "click", "Name": "Thumb Query", "key": "Button_1 "},  {" Type ":" click "," Name ":" Thumb Leave "," key ":" Button_2 "},  {" type ":" View "," name ":" Work Number binding "," url ":"/HTTP/ Xxxxxxxxxxxxxxxxx "}]},  {" Type ":" click "," Name ":" Orchestration "," Key ":" Button_3 "},  {" Name ":" Employee Suggestion "," Sub_button ": [   { "Type": "View", "name": "Spark of Thought", "url": "Http://XXXXXXXXXXXXXXXXXX"},   {"type": "View", "Name": "Prize Redemption", "url": "http:/ /xxxxxxxxxxxxxxxxxx "},   {" Type ":" click "," Name ":" Likes Us "," Key ":" Button_zan "}]  }]}

Parameter description

return results
The correct return JSON packet is as follows:
{"Errcode": 0, "errmsg": "OK"}
The return JSON packet On error is as follows (example is invalid menu name length):
{"Errcode": 40018, "errmsg": "Invalid button name size"}

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 Message", "Sub_button": [   {"Type": "click", "Name": "Thumb Query", "key": "Button_1 "},  {" Type ":" click "," Name ":" Thumb Leave "," key ":" Button_2 "},  {" type ":" View "," name ":" Work Number binding "," url ":"/HTTP/ Xxxxxxxxxxxxxxxxx "}]},  {" Type ":" click "," Name ":" Orchestration "," Key ":" Button_3 "},  {" Name ":" Employee Suggestion "," Sub_button ": [   { "Type": "View", "name": "Spark of Thought", "url": "Http://XXXXXXXXXXXXXXXXXX"},   {"type": "View", "Name": "Prize Redemption", "url": "http:/ /xxxxxxxxxxxxxxxxxx "},   {" Type ":" click "," Name ":" Likes Us "," Key ":" Button_zan "}]  }]}

Responding 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&A   mp;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, could 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 (); Text message Template $TEXTTPL = "<xml> <tousername><! [cdata[%s]]></tousername> <fromusername><! [cdata[%s]]></fromusername> <CreateTime>%s</CreateTime> <msgtype><! [cdata[%s]]&Gt;</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 a 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"://Picture Message $msgType = "text";     $CONTENTSTR = "$fromUsername, you sent the picture information";     $RESULTSTR = sprintf ($TEXTTPL, $fromUsername, $toUsername, $time, $msgType, $CONTENTSTR);    Break     Case "Voice"://Voice Message $msgType = "text";     $CONTENTSTR = "$fromUsername, you send a sound message"; $RESULTSTR = sprintf ($TEXTTPL, $fromUsername, $toUsername, $time, $msgType, $cONTENTSTR);    Break     Case "video"://Video message $msgType = "text";     $CONTENTSTR = "$fromUsername, you sent a video message";     $RESULTSTR = sprintf ($TEXTTPL, $fromUsername, $toUsername, $time, $msgType, $CONTENTSTR);    Break     Case ' location '://Position message $msgType = "text";     $CONTENTSTR = "$fromUsername, you sent 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);    Break Case "Event"://Events switch (strtolower (Trim ($postObj->event))) {case "subscribe"://concern $msgType = "Tex       T ";       $CONTENTSTR = "Welcome to your attention xxxxxxx";       $RESULTSTR = sprintf ($TEXTTPL, $fromUsername, $toUsername, $time, $msgType, $CONTENTSTR);      Break      Case "unsubscribe"://Cancel attention event break;     Case "Scan"://user is concerned when scanning QR code event $msgType = "text";  $CONTENTSTR = "$fromUsername, you scanned the QR code";       $RESULTSTR = sprintf ($TEXTTPL, $fromUsername, $toUsername, $time, $msgType, $CONTENTSTR);      Break       Case ' location '://upload geolocation event $msgType = "text";       $CONTENTSTR = "$fromUsername, you upload geographic location";       $RESULTSTR = sprintf ($TEXTTPL, $fromUsername, $toUsername, $time, $msgType, $CONTENTSTR);      Break       Case "click"://Custom Menu Event $msgType = "text";       $CONTENTSTR = "$fromUsername, you clicked on the custom menu $postObj->eventkey"; if ("button_zan" = = $postObj->eventkey) {$contentStr = "Thank you for your praise, we will continue to provide better quality service."       ";       } $resultStr = sprintf ($TEXTTPL, $fromUsername, $toUsername, $time, $msgType, $CONTENTSTR);       ;      Break     Default: $resultStr = "";    } break;   Default: $resultStr = "";  } Echo $resultStr; }}?>

customizing menu Queries

Using interfaces to create custom menus, developers can also use interfaces to query the structure of custom menus.

Request Description
HTTP request mode: GET
Https://api.weixin.qq.com/cgi-bin/menu/get?access_token=ACCESS_TOKEN

Return description
corresponding to the creation interface, the correct JSON returns the result:

Copy the Code code as follows:

{"Menu": {"button": [{"Name": "My Message", "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 Number binding", "url": "http:\/\/ XXXXXXXX "," Sub_button ": []}]},{" Type ":" click "," Name ":" Orchestration "," Key ":" Button_3 "," Sub_button ": []},{" Name ":" Employee Suggestion "," Sub_button ": [{" Type ":" View "," name ":" Thought Spark "," url ":" Http:\/\/xxxxxxxx "," Sub_button ": []},{" type ":" View "," Name ":" Prize Redemption "," url ":" Http:\/\/xxxxxxxx "," Sub_button ": []},{" Type ":" click "," Name ":" Likes Us "," Key ":" Button_zan "," Sub_button ":[]}]}]}}

Customizing Menu Deletions

After creating a custom menu using an interface, developers can also use the interface to delete the custom menus that are currently in use.

Request Description
HTTP request mode: GET
Https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=ACCESS_TOKEN

Return description
corresponding to the creation interface, the correct JSON returns the result:
{"Errcode": 0, "errmsg": "OK"}

Summary: The above is the entire content of this article, I hope to be able to help you learn.

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.