Overview of a custom menu
Custom menus Help the public number to enrich the interface, allowing users to better and faster understand the function of the public number. When you open a custom menu, the public number screen looks like this:
Second, the Application custom menu
Personal subscription number using micro-blog authentication, enterprise subscription number through micro-letter certification; can apply to custom menu eligibility
The service number defaults to a menu permission.
Iii. access to AppID and Appsecert
AppID and Appsecret can be found in the developer center-developer ID.
Iv. Access to access Token
Access token is obtained with AppID and Appsecert, and the interface is
Https://api.weixin.qq.com/cgi-bi ... Mp;secret=appsecret
The program is implemented as follows
$appid = "";
$appsecret = "";
$url = "Https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid= $appid &secret=$ Appsecret ";
$ch = Curl_init ();
curl_setopt ($ch, Curlopt_url, $url);
curl_setopt ($ch, Curlopt_ssl_verifypeer, FALSE);
curl_setopt ($ch, Curlopt_ssl_verifyhost, FALSE);
curl_setopt ($ch, Curlopt_returntransfer, 1);
$output = curl_exec ($ch);
Curl_close ($ch);
$jsoninfo = Json_decode ($output, true);
$access _token = $jsoninfo ["Access_token"];
You can also directly in the browser address bar, stitching out the address, after execution, to obtain the following data
Copy Code code as follows:
{"Access_token": "N2l7kxa084wvelonyjkj_trabmccvy_ukmpuuzlrq0ea2ynp3iz6esurrg0bhar_ Viswd50vdupky5ng43d1gbm-olt2krmxosve08rfed9lvk9lmgung9kpikkgzejif8jv2m9ffhf8bnna-yqh3g ",
Copy Code code as follows:
The parameter description is as follows
of which
N2l7kxa084wvelonyjkj_trabmccvy_ukmpuuzlrq0ea2ynp3iz6esurrg0bhar_ viswd50vdupky5ng43d1gbm-olt2krmxosve08rfed9lvk9lmgung9kpikkgzejif8jv2m9ffhf8bnna-yqh3g
is access token.
Or use the Official interface debugging tool, the address is:
Https://mp.weixin.qq.com/debug/cgi-bin/apiinfo?t=index&type=%E8%87%AA%E5%AE%9A%E4%B9%89%E8%8F%9C%E5%8D%95 &form=%e8%87%aa%e5%ae%9a%e4%b9%89%e8%8f%9c%e5%8d%95%e5%88%9b%e5%bb%ba%e6%8e%a5%e5%8f%a3%20/menu/create
Debugging a custom menu interface using the Web Debugging tool
Click to check the problem to get
This also gets access token
V. Organize menu content
Currently, custom menus include up to 3 levels of menus, and each level menu contains up to 5 levels of two menus. 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 method: POST (please use HTTPS protocol) Https://api.weixin.qq.com/cgi-bi ... _token=access_token
Request sample
{"
button": [
{
"type": "Click",
"name": "Today's song",
"key": "V1001_today_music"
},
{
" Type ': ' Click ',
' name ': ' Profile of singer ',
' key ': ' V1001_today_singer '
},
{
' name ': ' Menu ',
' sub_ Button ': [
{
' type ': ' View ',
' name ': ' Search ',
' url ': ' http://www.soso.com/'
},
{
' Type ': ' View ',
' name ': ' Video ',
' url ': ' http://v.qq.com/'
},
{
' type ': ' Click ',
' name ': "Praise Us",
"key": "V1001_good"
}]
}
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"}
Vi. submitting menu content to the server
The JSON structure of the menu is
{' button ': [{' name ': ' Weather forecast ', ' Sub_button ': [{' Type ': ' Click ', ' name ': ' Beijing weather ', ' key ': ' Weather Beijing '},
{' type ': ' Click ', ' name ":" Shanghai Weather "," key ": Weather Shanghai},
{" Type ":" click "," Name ":" Guangzhou Weather "," key ":" Weather Guangzhou "},{" type ":" click "," Name ":" Shenzhen Weather "," Key ":" Weather Shenzhen "},
{" type ":" View "," name ":" Local Weather "," url ":" Http://m.hao123.com/a/tianqi "}]},
{" name ":" Square times Studio "," Sub_ Button ': [{' Type ': ' Click ', ' Name ': ' Introduction ', ' key ': ' Company '},
Save the following code as menu.php and run the file in the browser (for example, http://127.0.0.1/menu.php) and submit the menu directly to the micro-trust server
php $access _token = "";
$jsonmenu = ' {button ': [{' name ': ' Weather forecast ', ' Sub_button ': [{' Type ': ' Click ', ' name ': ' Beijing weather ',
"Key": "Weather Beijing"}, {"Type": "click", "Name": "Shanghai Weather", "key": "Weather Shanghai"}, {"Type": "Click",
"Name": "Guangzhou Weather", "key": "Weather Guangzhou"}, {"Type": "click", "Name": "Shenzhen Weather", "key": "Weather Shenzhen"}, {
' Type ': ' View ', ' name ': ' Local weather ', ' url ': ' Http://m.hao123.com/a/tianqi '}]}, {' name ': ' Snow ',
' Sub_button ': [{' Type ': ' Click ', ' Name ': ' Company profile ', ' key ': ' Companies '}, {' Type ': ' Click ',
"Name": "Fun Game", "Key": "Game"}, {"Type": "click", "Name": "Tell a joke", "key": "Joke"}]}
}';
$url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=". $access _token;
$result = Https_request ($url, $jsonmenu);
Var_dump ($result);
function Https_request ($url, $data = null) {$curl = Curl_init (); Curl_setOpt ($curl, Curlopt_url, $url);
curl_setopt ($curl, Curlopt_ssl_verifypeer, FALSE);
curl_setopt ($curl, Curlopt_ssl_verifyhost, FALSE);
if (!empty ($data)) {curl_setopt ($curl, Curlopt_post, 1);
curl_setopt ($curl, Curlopt_postfields, $data);
curl_setopt ($curl, Curlopt_returntransfer, 1);
$output = curl_exec ($curl);
Curl_close ($curl);
return $output;
}?>
or use the Web Debugging tool to debug the interface using the official debugging interface
After the submission is successful, you can see the menu again after you pay attention. The menu effect is similar to the following:
Response Menu Click event
In the message interface to handle event events, where the click on behalf of the menu click, by responding to the menu structure key value Response message, view event no response, will jump directly to the past
Define ("TOKEN", "Weixin");
$WECHATOBJ = new Wechatcallbackapitest ();
if (!isset ($_get[' echostr ')) {$WECHATOBJ->responsemsg ();
}else{$wechatObj->valid ();
Class Wechatcallbackapitest {public function valid () {$echoStr = $_get["Echostr"];
if ($this->checksignature ()) {echo $echoStr;
Exit
The Private Function checksignature () {$signature = $_get["signature"];
$timestamp = $_get["timestamp"];
$nonce = $_get["nonce"];
$token = token;
$TMPARR = Array ($token, $timestamp, $nonce);
Sort ($TMPARR);
$TMPSTR = implode ($TMPARR);
$TMPSTR = SHA1 ($TMPSTR);
if ($tmpStr = = $signature) {return true;
}else{return false;
The Public Function responsemsg () {$postStr = $GLOBALS ["Http_raw_post_data"];
if (!empty ($POSTSTR)) {$POSTOBJ = simplexml_load_string ($postStr, ' simplexmlelement ', libxml_nocdata); $RX _type = Trim ($postObJ->msgtype);
Switch ($RX _type) {case "text": $resultStr = $this->receivetext ($POSTOBJ);
Break
Case "Event": $resultStr = $this->receiveevent ($POSTOBJ);
Break
Default: $resultStr = "";
Break
Echo $resultStr;
}else {echo "";
Exit
The Private Function Receivetext ($object) {$funcFlag = 0;
$CONTENTSTR = "The content you send is:". $object->content;
$RESULTSTR = $this->transmittext ($object, $CONTENTSTR, $funcFlag);
return $resultStr;
Private Function Receiveevent ($object) {$contentStr = "";
Switch ($object->event) {case "subscribe": $contentStr = "Welcome to Yangyang Blog";
Case "unsubscribe": break; Case "click": Switch ($object->eventkey) {case ' company ': $contentStr [] = AR
Ray ("Title" => "Company Profile","Description" => "Yangyang's Blog", "Picurl" => "Http://discuz.comli.com/weixin/weather/icon/cartoon.jpg",
"Url" => "Weixin://addfriend/pondbaystudio");
Break Default: $contentStr [] = Array ("Title" => "default menu Reply", "Description" => "you are using <span style = "Font-family:arial, Helvetica, Sans-serif;" > Yangyang's blog </span><span style= "font-family:arial, Helvetica, Sans-serif;" > ", </span>" Picurl "=>" http://discuz.comli.com/weixin/weather/icon/cartoon.jpg ","
Url "=>" Weixin://addfriend/pondbaystudio ");
Break
} break;
Default:break;
} if (Is_array ($CONTENTSTR)) {$resultStr = $this->transmitnews ($object, $CONTENTSTR);
}else{$resultStr = $this->transmittext ($object, $CONTENTSTR);
return $resultStr;
Private Function Transmittext ($object, $content, $funcFlag = 0){$TEXTTPL = "<xml> <tousername><! [cdata[%s]]></tousername> <fromusername><! [cdata[%s]]></fromusername> <CreateTime>%s</CreateTime> <msgtype><! [cdata[text]]></msgtype> <content><!
[cdata[%s]]></content> <FuncFlag>%d</FuncFlag> </xml> ";
$RESULTSTR = sprintf ($TEXTTPL, $object->fromusername, $object->tousername, Time (), $content, $funcFlag);
return $resultStr; Private Function Transmitnews ($object, $arr _item, $funcFlag = 0) {//First title 28 word, other title 39 Word if (!is_array ($arr
_item)) return; $ITEMTPL = "<item> <title><! [cdata[%s]]></title> <description><! [cdata[%s]]></description> <picurl><! [cdata[%s]]></picurl> <url><!
[cdata[%s]]></url> </item> ";
$item _str = ""; foreach ($arr _item as $item) $item _str. = SprinTF ($ITEMTPL, $item [' Title '], $item [' Description '], $item [' Picurl '], $item [' Url ']); $NEWSTPL = "<xml> <tousername><! [cdata[%s]]></tousername> <fromusername><! [cdata[%s]]></fromusername> <CreateTime>%s</CreateTime> <msgtype><! [cdata[news]]></msgtype> <content><! [cdata[]]></content> <ArticleCount>%s</ArticleCount> <Articles> $item _str</articles
> <FuncFlag>%s</FuncFlag> </xml> "; $RESULTSTR = sprintf ($NEWSTPL, $object->fromusername, $object->tousername, Time (), COUNT ($arr _item), $funcFlag)
;
return $resultStr;
}}?>
Eight, the menu to get OpenID
Because the menu can only fill in a fixed URL address, for the menu to get the user's OpenID, you can use the OAuth2.0 authorization to achieve.
The address that is filled in in the URL is a fixed callback address. Principle method can refer to micro-credit public platform development (99) custom menu to get OpenID
<?php/* Yangyang's Blog * * Define ("TOKEN", "Weixin");
$WECHATOBJ = new Wechatcallbackapitest ();
if (Isset ($_get[' echostr ')) {$WECHATOBJ->valid ();
}else{$wechatObj->responsemsg ();
Class Wechatcallbackapitest {public function valid () {$echoStr = $_get["Echostr"];
if ($this->checksignature ()) {header (' content-type:text ');
Echo $echoStr;
Exit
The Private Function checksignature () {$signature = $_get["signature"];
$timestamp = $_get["timestamp"];
$nonce = $_get["nonce"];
$token = token;
$TMPARR = Array ($token, $timestamp, $nonce);
Sort ($TMPARR, sort_string);
$TMPSTR = implode ($TMPARR);
$TMPSTR = SHA1 ($TMPSTR);
if ($tmpStr = = $signature) {return true;
}else{return false;
The Public Function responsemsg () {$postStr = $GLOBALS ["Http_raw_post_data"]; if (!empty ($POSTSTR)) {$POSTOBJ = Simplexml_load_stRing ($POSTSTR, ' simplexmlelement ', libxml_nocdata);
$fromUsername = $POSTOBJ->fromusername;
$toUsername = $POSTOBJ->tousername;
$keyword = Trim ($postObj->content);
$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> "; if ($keyword = = "?" | | $keyword = = "?
") {$msgType =" text ";
$CONTENTSTR = ' The current time is: '. Date ("Y-m-d h:i:s", Times ());
$RESULTSTR = sprintf ($TEXTTPL, $fromUsername, $toUsername, $time, $msgType, $CONTENTSTR);
Echo $resultStr;
}}else{echo "";
Exit }}}?>
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.