WeChat Official Account click menu to open and log on to the microsite implementation method

Source: Internet
Author: User
Tags openid
This article mainly introduces how to open and log on to the microsite by clicking the public account menu, the implementation steps and functional code of callback, menu, and parameter processing are described in detail in the form of instances, this article describes how to open and log on to the microsite by clicking the public account menu. Share it with you for your reference. The specific analysis is as follows:

In general, the steps for opening and logging on to the microsite by clicking the public account menu are complicated, but many microsites are using their own resources. This article summarizes these steps, I believe this will bring you some reference value.

Currently, most microsites use the user's openid for automatic logon. In my previous development, a user clicks a menu, and the public number returns a text. the user clicks this text to automatically log on to the microsite. However, if you have an advanced interface, you can click the menu and open the web page to obtain this openid, enabling automatic logon.
As mentioned above, you must have permissions for advanced interfaces (service numbers and enterprise numbers) and enable the developer mode.

1. set the callback address

Find "OAuth2.0 webpage authorization" under "Advanced interface" in the "Developer Center" of the public platform background, and click "modify" next to it. the dialog box for entering the callback address is displayed. For details about how to authorize, click here to learn. Only after obtaining the advanced interface permission can the "modification" of this place appear ".
Note: the domain name is entered here, not the URL with it, and the explanation is very clear, "The authorization callback domain name configuration specification is full domain name ", that is to say, domain names with and without www are different. Therefore, I want to enter the domain name as shown in.

2. create a menu

You can create a menu in the background of your website. if the developer mode is not enabled, you can also create a menu in the background of the public platform.
Click the link to open the menu, that is, view mode. If you are using the developer mode, you can create a public account menu (Developer Documentation) by submitting the following code ):

The code is as follows:

{
"Button ":[
{
"Type": "view ",
"Name": "Log on to the microsite ",
"Url": "https://open.weixin.qq.com/connect/oauth2/authorize? Appid = {get this APPID in the background of the public platform} & redirect_uri = {address under the callback domain name you entered} & response_type = code & scope = snsapi_base & state = 1 # wechat_redirect"
}]
}


Code 1: The menu code to be submitted.
The location where APPID is obtained is the "Developer Center" where you fill in the callback address ". Below we use PHP to implement menu submission:

The code is as follows:

<? Php
Function curl_info ($ appid, $ secret ){
$ Ch = curl_init ();
Curl_setopt ($ ch, CURLOPT_URL, "https://api.weixin.qq.com/cgi-bin/token? Grant_type = client_credential & appid = ". $ appid." & secret = ". $ secret );
Curl_setopt ($ ch, CURLOPT_CUSTOMREQUEST, "GET ");
Curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, FALSE );
Curl_setopt ($ ch, CURLOPT_SSL_VERIFYHOST, FALSE );
Curl_setopt ($ ch, CURLOPT_USERAGENT, 'mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0 )');
Curl_setopt ($ ch, CURLOPT_FOLLOWLOCATION, 1 );
Curl_setopt ($ ch, CURLOPT_AUTOREFERER, 1 );
// Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ data );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true );
$ TmpInfo = curl_exec ($ ch );
If (curl_errno ($ ch )){
Echo 'errno'. curl_error ($ ch );
}
Curl_close ($ ch );
$ Arr = json_decode ($ tmpInfo, true );
Return $ arr;
}
Function curl_menu ($ ACCESS_TOKEN, $ data ){
$ Ch = curl_init ();
Curl_setopt ($ ch, CURLOPT_URL, "https://api.weixin.qq.com/cgi-bin/menu/create? Access_token = ". $ ACCESS_TOKEN );
Curl_setopt ($ ch, CURLOPT_CUSTOMREQUEST, "POST ");
Curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, FALSE );
Curl_setopt ($ ch, CURLOPT_SSL_VERIFYHOST, FALSE );
Curl_setopt ($ ch, CURLOPT_USERAGENT, 'mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0 )');
Curl_setopt ($ ch, CURLOPT_FOLLOWLOCATION, 1 );
Curl_setopt ($ ch, CURLOPT_AUTOREFERER, 1 );
Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ data );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true );
$ TmpInfo = curl_exec ($ ch );
If (curl_errno ($ ch )){
Echo 'errno'. curl_error ($ ch );
}
Curl_close ($ ch );
$ Arr = json_decode ($ tmpInfo, true );
Return $ arr;
}
Function creat_menu (){
$ ACCESS_LIST = curl_info (APP_ID, APP_SCR); // The obtained credential. you need to define the APP_ID and APP_SCR (Application key) by yourself. this is also found in the public platform background Developer Center.
If ($ ACCESS_LIST ['Access _ token']! = ''){
$ Access_token = $ ACCESS_LIST ['Access _ token']; // get ACCESS_TOKEN
$ Data = 'copy and paste the above code 1 here ';
$ Msg = curl_menu ($ access_token, preg_replace ("# u ([0-9a-f] +) # ie", "iconv ('ucs-2', 'utf-8 ', pack ('h4 ', '1') ", $ data ));
If ($ msg ['errmsg '] =' OK '){
Die ('custom menu created successfully! ');
}
Else {
Die ('failed to create custom menu! ');
}
}
Else {
Die ('creation failed, incorrect filling of AppId or AppSecret ');
}
}
Create_menu ();
?>


Code 2 use PHP to create a public account menu

Code 2 is actually a bit redundant, and the core part is marked in red. In this way, you should soon be able to see the creation of a "log on to the microsite" menu in your public account. Click this menu to log on to the microsite.
If you do not need PHP, you can directly write links in the menu customization of the public platform background.

In, select open link to create a menu. OK. Then, put the above link in:

Https://open.weixin.qq.com/connect/oauth2/authorize? Appid = {get this APPID in the background of the public platform} & redirect_uri = {address under the callback domain name you entered} & response_type = code & scope = snsapi_base & state = 1 # wechat_redirect

Create a menu.
Of course, you may only need to add this link in your own management background.

3. obtain the openid on the callback page.

You may have noticed that the above link address contains the parameter scope = snsapi_base, rather than scope = snsapi_userinfo, because you do not need to click an authorization button to use the former, directly jump to the callback page, and the latter needs to click the authorization button. However, clicking the authorization button is advantageous. First, you can authorize the callback without paying attention to the public account, second, some user information, such as nickname, gender, and location, can be obtained after authorization. But we want to use openid for login, so we can directly select the former.

After clicking the menu, the authorize processing will jump to the callback address you submitted (here you need to remind me that the callback address should not contain parameters, such as xxx /? Callback = from_weixin, because the callback address must also contain a parameter, which you need ). Jump to the following URL:
Callback address /? Code = CODE & state = 1

The code above can GET a CODE value through $ _ GET ['code']. with this CODE value and appid, you can GET the openid and access_token.
The following uses PHP to implement the following:

The code is as follows:

If ($ _ GET ['code']) {
$ Code = $ _ GET ['code'];
$ Data = get_by_curl ('https: // api.weixin.qq.com/sns/oauth2/access_token? Appid = APPID & secret = effecrc & code = '. $ code.' & grant_type = authorization_code ');
$ Data = json_decode ($ data );
$ Openid = $ data-> openid;
$ Access_token = $ data-> access_token;
}
Function get_by_curl ($ url, $ post = false ){
$ Ch = curl_init ();
Curl_setopt ($ ch, CURLOPT_URL, $ url );
Curl_setopt ($ ch, CURLOPT_HEADER, 0 );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
If ($ post ){
Curl_setopt ($ ch, CURLOPT_POST, 1 );
Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ post );
}
$ Result = curl_exec ($ ch );
Curl_close ($ ch );
Return $ result;
}


In this way, we can obtain the openid and access_token. with these values, we can also use the public platform's api interface to obtain basic user information.

I hope this article will help you develop a public account based on PHP.

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.