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 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 number and enterprise number) 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

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.