PHP micro-Credit Development custom menu _php Tips

Source: Internet
Author: User

The current micro-service number customization menu includes up to 3 levels of menus, each with a maximum of 5 levels of menu two. The first level menu up to 4 characters, two level menu up to 7 characters, the more out of the part will be "..." instead. Note that when you create a custom menu, a 24-hour micro-credit client is required to be displayed due to the micro-client cache. When it is recommended that you try to cancel the attention of the public account after the test, you can see the effect after the creation.

The current custom menu interface can implement two types of buttons, as follows:
Click:
After the user clicks the Click Type button, the micro-trust server will push the message type to the developer (Reference Message Interface Guide) via the message interface, and with the key value that the developer fills in the button, the developer can interact with the user through the custom key value.
View:
The user clicks the View Type button, the micro-letter client will open the developer to fill in the button the URL value (namely the webpage link), achieves opens the webpage the goal, proposed with the webpage authorization obtains the user basic information interface union, obtains the user the login personal information.

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

 {'
 button ': [
  {' type ': ' Click ', ' name ': ' My info ', ' 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 ': ' Business process ', ' key ': ' Button_3 '},
  {' name ': ' Employee recommendation ', ' sub_ Button ': [
   {' type ': ' View ', ' name ': ' Thought spark ', ' url ': ' Http://XXXXXXXXXXXXXXXXXX '},
   {' type ': ' View ', ' name ': ' Prize Redemption ", url": "Http://XXXXXXXXXXXXXXXXXX"},
   {"Type": "click", "Name": "Praise Us", "Key": "Button_zan"}]
  }
 ]
}

Parameter description

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

The following is 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 info ', ' 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 ': ' Business process ', ' key ': ' Button_3 '},
  {' name ': ' Employee recommendation ', ' sub_ Button ': [
   {' type ': ' View ', ' name ': ' Thought spark ', ' url ': ' Http://XXXXXXXXXXXXXXXXXX '},
   {' type ': ' View ', ' name ': ' Prize Redemption ", url": "Http://XXXXXXXXXXXXXXXXXX"},
   {"Type": "click", "Name": "Praise 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; The Private Function Getaccesstoken () {$url = "Https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credenti

   al&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;
   
   The public function valid () {$echoStr = $_get["Echostr"];
    Valid signature, option if ($this->checksignature ()) {echo $echoStr;
   Exit The Public Function responsemsg () {//get post data, May is due to the different environments $postStr = $GLOB
   als["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]]></msgtype> <content><!
   
   [cdata[%s]]></content> <FuncFlag>0</FuncFlag> </xml> ";
     Switch (Strtolower ($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 sent a sound message ";
     $RESULTSTR = sprintf ($TEXTTPL, $fromUsername, $toUsername, $time, $msgType, $CONTENTSTR);
    Break
     Case "video":///$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 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);
    Break Case "Event"://Event switch (Strtolower (Trim ($postObj->event)) {cases "subscribe"://focus on Event $msgType = "
       Text ";
       $CONTENTSTR = "Welcome your concern xxxxxxx"; $RESULTSTR = sprintf ($TEXTTPL, $fromUsername, $toUsername, $time, $msgType, $contenTSTR);
      Break
      Case "unsubscribe"://Cancel the attention event break;
       Case "Scan"://user has been concerned when scanning two-dimensional code event $msgType = "text";
       $CONTENTSTR = "$fromUsername, you scanned the two-dimensional code";
       $RESULTSTR = sprintf ($TEXTTPL, $fromUsername, $toUsername, $time, $msgType, $CONTENTSTR);
      Break
       Case "Location"://Uploading geographical event $msgType = "text";
       $CONTENTSTR = "$fromUsername, you upload geographical 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 more quality service."
       ";
       $RESULTSTR = sprintf ($TEXTTPL, $fromUsername, $toUsername, $time, $msgType, $CONTENTSTR);
       ;
      Break
     Default: $resultStr = "";
    } break;
   Default: $resultStr = "";
  Echo $resultStr;

 }}?>

Custom menu Query

With 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 Create interface, the correct JSON returns the result:

Copy Code code as follows:
{"Menu": {"button": [{"Name": "My Information", "Sub_button": [{":"] [{"]", "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 ":" Business Process "," 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 ":" Praise Us "," Key ":" Button_zan "," Sub_button ":[]}]}]}}

custom Menu Deletion

After creating a custom menu using an interface, developers can also use the interface to remove the currently used custom menu.

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

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

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.