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:
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:
I hope this article will be helpful to everyone in developing WeChat public accounts based on PHP.