Keyword public platform custom menu OpenID
Author: Fang Times studio
Original article: http://www.cnblogs.com/txw1958/p/weixin-menu-get-openid.html
In this public platform development tutorial, we will introduce how to obtain the user's OpenID in the custom menu.
The essence of this development tutorial is the flexible use of custom menus and OAuth2.0 authorization.
This article consists of the following four parts:
1. Have advanced interface Permissions
When you have advanced interface permissions, we use OAuth2.0 authorization to obtain them.
For details about how to set OAuth2.0, refer to public platform development (71) OAuth2.0 webpage authorization
After setting the domain name on the Authorization callback page, we need to construct a menu. To facilitate the simultaneous explanation of the next section, the menu structure is as follows:
{"Button": [{"type": "click", "name": "graphic acquisition", "key": "graphic" },{ "type ": "view", "name": "get authorization", "url": "https://open.weixin.qq.com/connect/oauth2/authorize? Appid = wx0738856156f7323c & redirect_uri = http://israel.sinaapp.com/oauth2/&response_type=code&scope=snsapi_base&state=1#wechat_redirect "}]}
Two menus are generated as follows:
When you click authorize to obtain the openid, the authorization page is accessed and the OAuth2.0 method is used to obtain the openid.
For the code, see page 72nd of public platform development best practices.
The following figure is displayed after the jump:
2. No Advanced Interface permission
For accounts without advanced interface permissions, you cannot use view events to obtain OpenID. You need to use click events to implement
The implementation principle is to obtain the FromUserName value in the received message and construct the openid parameter in the url.
The response code in the custom menu is as follows:
1 private function receiveEvent ($ object) 2 {3 $ content = ""; 4 switch ($ object-> Event) 5 {6 case "subscribe ": 7 $ content [] = array ("Title" => "Welcome to fantimes Studio", "Description" => "", "PicUrl" => "http://discuz.comli.com/weixin/weather/icon/cartoon.jpg ", "Url" => "http://m.cnblogs.com /? U = txw1958 "); 8 break; 9 case" CLICK ": 10 switch ($ object-> EventKey) 11 {12 case" ": 13 $ content [] = array ("Title" => "OpenID", "Description" => "Your OpenID is :". $ object-> FromUserName, "PicUrl" => "", "Url" => "http://m.cnblogs.com /? U = txw1958 & openid = ". $ object-> FromUserName); 14 break; 15} 16 break; 17} 18 if (is_array ($ content) {19 $ result = $ this-> transmitNews ($ object, $ content); 20} else {21 $ result = $ this-> transmitText ($ object, $ content); 22} 23 return $ result; 24}
The effect after running is as follows.
========================================================== ====
Follow-up methods for fanwe public platform accounts:
1. Address Book-Add friends-search for Public Accounts-search for "fangtimes Studio"
2. Address Book-add friend-search number-Enter "pondbaystudio"
3. Scan the following QR code