How to obtain OpenID_PHP from custom menu of WeChat public platform development (99)

Source: Internet
Author: User
Obtain the OpenID from the custom menu of public platform development (99. Keyword public platform custom menu OpenID author: Fang times studio original: www.cnblogs.comtxw1958pweixin-menu-get-openid.html in this public platform development teach keywords 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

Author: Fang times studio Original article: http://www.cnblogs.com/txw1958/p/weixin-menu-get-openid.html in this public platform development teaching...

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.