


How to implement the WeChat public account to open and log in to the microsite by clicking on the menu, public_PHP tutorial
How to implement the WeChat public account to open and log in to the microsite by clicking on the menu, public
The example in this article describes how to implement the WeChat public account to open and log in to the microsite by clicking on the menu. Share it with everyone for your reference. The specific analysis is as follows:
Generally speaking, the steps to implement the WeChat official account by clicking on the menu to open and log in to the microsite are relatively complicated, but many microsites have been used. This article summarizes this, and I believe it can bring some reference to everyone. Reference value.
Nowadays, most microsites realize automatic login through the user’s WeChat openid. In my previous development, the user clicked on a menu, and the official account returned an image and text. Only when the user clicked on this image and text could they automatically log in to the microsite. But if you have an advanced interface, you can click on the menu and open the web page to get the openid and realize automatic login.
As mentioned here, you must have the permissions of the advanced interface (service account, enterprise account) and turn on the developer mode.
1. Set callback address
In the "Developer Center" of the WeChat public platform background, find "OAuth2.0 Web Authorization" under "Advanced Interface". There is a "Modify" after it. After clicking, a dialog box for filling in the callback address will pop up. For details on how to authorize, please click here to learn. Only after obtaining the advanced interface permission can the "modification" of this place appear.
Note that the domain name filled in here is the domain name, not the URL, and the explanation is very clear, "The authorization callback domain name configuration specification is the full domain name", which means that the domain name with www and without it are two different domain names. Therefore, I need to fill in the domain name as shown below.
2. Create menu
The creation menu can be created through the backend of your microsite. If the developer mode is not turned on, it can also be created through the backend of the WeChat public platform.
The menu uses the click-to-open link mode, which is the view mode. If you are using developer mode, you can create a public account menu (developer documentation) by submitting the following code to WeChat:
"button":[
{
"type":"view",
"name":"Log in to the microsite",
"url":"https://open.weixin.qq.com/connect/oauth2/authorize?appid={Get this APPID in the WeChat public platform background}&redirect_uri={The address under the callback domain name you filled in}&response_type=code&scope =snsapi_base&state=1#wechat_redirect"
}]
}
Code 1 Menu code to be submitted,
will be used below The location to obtain the APPID is the "Developer Center" where you filled in the callback address above. Next we use PHP to implement menu submission:
$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);//获取到的凭证,你需要自己define APP_ID和APP_SCR(应用密钥),这个也是在微信公众平台后台开发者中心找
if($ACCESS_LIST['access_token']!='') {
$access_token = $ACCESS_LIST['access_token'];//获取到ACCESS_TOKEN
$data = '把上面代码1拷贝黏贴在这里';
$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('创建自定义菜单成功!');
}
else {
die('创建自定义菜单失败!');
}
}
else {
die('创建失败,微信AppId或微信AppSecret填写错误');
}
}
create_menu();
?>
Code 2 Use PHP to create WeChat public account menu
Code 2 is actually a bit redundant, the core part is highlighted in red. In this way, you should soon see a "Login to Microsite" menu created in your WeChat official account. Click this menu to log in to the microsite.
If you don’t need PHP, you can just write the link directly in the menu customization in the background of the WeChat public platform.
In this place in the picture above, choose to open the link to create the menu. OK, now put the link above:
https://open.weixin.qq.com/connect/oauth2/authorize?appid={Get this APPID in the WeChat public platform background}&redirect_uri={The address under the callback domain name you filled in}&response_type=code&scope=snsapi_base&state =1#wechat_redirect
Just create the menu.
Of course, you may also just need to add this link to your own WeChat management background.
3. Get the openid on the callback page
If you are careful, you may have noticed that the above link address contains the parameter scope=snsapi_base instead of scope=snsapi_userinfo, because using the former does not require the user to click an authorization button and jump directly to the callback page, while the latter requires clicking Authorization button, but there are advantages to clicking the authorization button. First, you can authorize without following the official account. Second, after authorization, you can get some information about the user, such as nickname, gender, and location. But we are using openid to log in, so just choose the former.
After clicking the menu, after WeChat authorize processing, it will jump to the callback address you submitted (it needs to be reminded here that the callback address is best not to take parameters, such as xxx/?callback=from_weixin, because WeChat jumps to your The callback address also needs parameters, and this parameter is what you need). WeChat jumps to the following URL:
Callback address/?code=CODE&state=1
The above code can obtain a CODE value through $_GET['code']. Using this CODE value and appid, openid and access_token can be obtained.
Next, use PHP to implement the following:
$code = $_GET['code'];
$data = get_by_curl('https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=APPSRC&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, openid and access_token can be obtained. Using these values, we can also use the WeChat public platform's API interface to obtain basic user information.
I hope this article will be helpful to everyone in developing WeChat public accounts based on PHP.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Thanks to netizens Qing Qiechensi, HH_KK, Satomi Ishihara and Wu Yanzu of South China for submitting clues! According to news on September 2, there are recent rumors that "iPhone 16 may not support WeChat." In response to this, a reporter from Shell Finance called Apple's official hotline. Apple's technical consultant in China responded that whether iOS systems or Apple devices can continue to use WeChat, and WeChat The issue of whether it can continue to be listed and downloaded on the Apple App Store requires communication and discussion between Apple and Tencent to determine the future situation. Software App Store and WeChat Problem Description Software App Store technical consultant pointed out that developers may need to pay fees to put software on the Apple Store. After reaching a certain number of downloads, Apple will need to pay corresponding fees for subsequent downloads. Apple is actively communicating with Tencent,

DeepSeek: A powerful AI image generation tool! DeepSeek itself is not an image generation tool, but its powerful core technology provides underlying support for many AI painting tools. Want to know how to use DeepSeek to generate images indirectly? Please continue reading! Generate images with DeepSeek-based AI tools: The following steps will guide you to use these tools: Launch the AI Painting Tool: Search and open a DeepSeek-based AI Painting Tool (for example, search "Simple AI"). Select the drawing mode: select "AI Drawing" or similar function, and select the image type according to your needs, such as "Anime Avatar", "Landscape"

Rumors of WeChat supporting iPhone 16 were debunked. Thanks to netizens Xi Chuang Jiu Shi and HH_KK for submitting clues! According to news on September 2, there are rumors today that WeChat may not support iPhone 16. Once the iPhone is upgraded to the iOS 18.2 system, it will not be able to use WeChat. According to "Daily Economic News", it was learned from people familiar with the matter that this rumor is a rumor. Apple's response: According to Shell Finance, Apple's technical consultant in China responded that the issue of whether WeChat can continue to be used on iOS systems or Apple devices, and whether WeChat can continue to be listed and downloaded in the Apple App Store, needs to be resolved between Apple and Tencent. Only through communication and discussion can we determine the future situation. Currently, Apple is actively communicating with Tencent to confirm whether Tencent will continue to

Gate.io, a leading cryptocurrency trading platform founded in 2013, provides Chinese users with a complete official Chinese website. The website provides a wide range of services, including spot trading, futures trading and lending, and provides special features such as Chinese interface, rich resources and community support.

The OKX trading platform offers a variety of rates, including transaction fees, withdrawal fees and financing fees. For spot transactions, transaction fees vary according to transaction volume and VIP level, and adopt the "market maker model", that is, the market charges a lower handling fee for each transaction. In addition, OKX also offers a variety of futures contracts, including currency standard contracts, USDT contracts and delivery contracts, and the fee structure of each contract is also different.

Gateio Exchange app download channels for old versions, covering official, third-party application markets, forum communities and other channels. It also provides download precautions to help you easily obtain old versions and solve the problems of discomfort in using new versions or device compatibility.

This article provides a detailed guide to safe download of Ouyi OKX App in China. Due to restrictions on domestic app stores, users are advised to download the App through the official website of Ouyi OKX, or use the QR code provided by the official website to scan and download. During the download process, be sure to verify the official website address, check the application permissions, perform a security scan after installation, and enable two-factor verification. During use, please abide by local laws and regulations, use a safe network environment, protect account security, be vigilant against fraud, and invest rationally. This article is for reference only and does not constitute investment advice. Digital asset transactions are at your own risk.

Gate.io (Sesame Open Door) is the world's leading cryptocurrency trading platform. This article provides a complete tutorial on spot trading of Gate.io. The tutorial covers steps such as account registration and login, KYC certification, fiat currency and digital currency recharge, trading pair selection, limit/market transaction orders, and orders and transaction records viewing, helping you quickly get started on the Gate.io platform for cryptocurrency trading. Whether a beginner or a veteran, you can benefit from this tutorial and easily master the Gate.io trading skills.
