


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.

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP makes it easy to create interactive web content. 1) Dynamically generate content by embedding HTML and display it in real time based on user input or database data. 2) Process form submission and generate dynamic output to ensure that htmlspecialchars is used to prevent XSS. 3) Use MySQL to create a user registration system, and use password_hash and preprocessing statements to enhance security. Mastering these techniques will improve the efficiency of web development.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.


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

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 English version
Recommended: Win version, supports code prompts!

SublimeText3 Chinese version
Chinese version, very easy to use

Dreamweaver Mac version
Visual web development tools