Home >Backend Development >PHP Tutorial >PHP implements WeChat web page login authorization development
This article mainly introduces the implementation of WeChat web page login authorization development with PHP. It has certain reference value. Now I share it with everyone. Friends in need can refer to it
WeChat open platform and the public The difference between platforms
1. The public platform is oriented to ordinary users, such as self-media and media, and is used by the company’s official WeChat public account operators. Of course, your team or company has the strength to develop some Content can also call interfaces in the public platform, such as custom menus, automatic replies, and query functions. At present, most WeChat are doing this after passing the certification.
mp.weixin.qq.com
2. The open platform is for developers and third-party independent software developers. I think the biggest openness of the development platform is WeChat login. At that time, Tencent did not make great efforts to implement unified login. As a result, each website now has to develop a login mechanism. Fortunately, they now understand the situation. Developers or software developers, through the platform and interface provided by WeChat, can develop e-commerce websites suitable for enterprises, scan the QR code to enter a game interface, and then purchase goods. Of course, the subsequent open platform will open the payment interface, so software developers such as Pocket Tong can provide services and software such as WeChat stores for large and small and medium-sized enterprises.
open.weixin.qq.com
The public platform is the management and development backend for service account subscription accounts.
To put it simply, the development platform is to realize the one-click sharing of the content of the software installed in the mobile phone to the circle of friends;
The following third-party login relies on the open platform (open.weixin.qq.com ) Function
Website application WeChat login is a WeChat OAuth2.0 authorized login system built based on the OAuth2.0 protocol standard.
Before performing WeChat OAuth2. Before performing WeChat OAuth2.0 authorized login and access, register a developer account on the WeChat open platform, have an approved website application, and obtain the corresponding AppID and AppSecret. After applying for WeChat login and passing the review, you can start the access process.
WeChat OAuth2.0 authorized login allows WeChat users to use their WeChat identity to securely log in to third-party applications or websites, and the WeChat user authorizes to log in to third parties that have accessed WeChat OAuth2.0 After application, the third party can obtain the user's interface calling credentials (access_token), and use the access_token to call the WeChat open platform authorization relationship interface, thereby obtaining the basic open information of WeChat users and helping users realize basic open functions.
WeChat OAuth2.0 authorized login currently supports authorization_code mode, which is suitable for application authorization with server side. The overall process of this model is:
1. A third party initiates a WeChat authorized login request. After the WeChat user allows authorization of the third-party application, WeChat will launch the application or redirect to the third-party website, and bring the authorization temporary ticket. code parameter;
2. Add AppID and AppSecret through the code parameter, and exchange access_token through API;
3. Make interface calls through access_token to obtain the user's basic data resources or help the user achieve basic operate.
Get access_token sequence diagram:
Third party use website application authorization Before logging in, please note that you have obtained the corresponding web page authorization scope (scope=snsapi_login), you can open the following link on the PC:
https://open.weixin.qq.com/connect/qrconnect?appid=APPID&redirect_uri= REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect
If it prompts "The link cannot be accessed", please check whether the parameters are filled in incorrectly. For example, the domain name of redirect_uri is inconsistent with the authorized domain name filled in during the review or the scope is not snsapi_login.
Parameters |
##Is it required |
Description |
is | Application Unique Identifier | |
is | Redirect address needs UrlEncode | |
is |
| Fill in the code|
is | Application authorization scope, multiple scopes are separated by commas (,), web applications currently only need to fill in snsapi_login | |
no | Used to maintain the status of requests and callbacks, and bring them back to the third party as they are after authorizing the request. This parameter can be used to prevent CSRF attacks (cross-site request forgery attacks). It is recommended that third parties bring this parameter. It can be set to a simple random number plus session for verification |
After the user allows authorization, it will be redirected to the redirect_uri URL with the code and state parameters
redirect_uri?code=CODE&state=STATE
If the user prohibits authorization, the code parameter will not be carried after redirection, only the state parameter will be carried
redirect_uri?state =STATE
Log in to Yihaodian website application
https://passport.yhd.com/wechat/login.do
After opening, Yihaodian will generate the state parameter and jump to
https://open.weixin.qq.com/connect/qrconnect?appid=wxbdc5610cc59c1631&redirect_uri=https://passport.yhd.com/wechat /callback.do&response_type=code&scope=snsapi_login&state=3d6be0a4035d839573b04816624a415e#wechat_redirect
https://open.weixin.qq.com/connect/qrconnect?appid=wxbdc5610cc59c1631&redirect_uri=https:// passport .yhd.com/wechat/callback.do&response_type=code&scope=snsapi_login&state=eb407f95fbc413185209fd85931761c2#wechat_redirect
After WeChat users use WeChat to scan the QR code and confirm login, the PC will jump to
https ://passport.yhd.com/wechat/callback.do?code=CODE&state=3d6be0a4035d839573b04816624a415e
The second way to obtain the code supports the website to log in to WeChat in 2D The code is embedded into the own page, and the user uses WeChat to scan the code for authorization and returns the code to the website through JS.
The main purpose of JS WeChat login: The website hopes that users can complete the login within the website without jumping to the WeChat domain to log in and then return, to improve the fluency and success rate of WeChat login. How to implement JS for WeChat login with QR code embedded in the website:
a41e9aa0571885bb940b7003c4385c9f2cacc6d41bbb37262a98f745aa00fbf0
varobj = new WxLogin({ id:"login_container", appid: "", scope: "", redirect_uri: "", state: "", style: "", href: "" });
##Parameter |
##Is it required
|
Description
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
is |
| The container id of the QR code displayed on the third-party page||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
is | The unique identification of the application will be obtained after submitting the application for review on the WeChat open platform. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
is the | application authorization scope. Multiple scopes are separated by commas (,). For web applications, currently you only need to fill in snsapi_login | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
is the | redirection address and needs to be UrlEncode | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
No | is used to maintain the status of the request and callback, after authorizing the request Bring it back to the third party as is. This parameter can be used to prevent CSRF attacks (cross-site request forgery attacks). It is recommended that third parties bring this parameter. It can be set to a simple random number plus session for verification | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
No | Provide "black" and "white" options, and the default is black text description. For details, see FAQ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
##No |
##Since Define style links, and third parties can override the default style according to actual needs. For details, see FAQ |
# at the bottom of the document Parameter DescriptionStep 2: Get access_token through codeGet access_token through code https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code Parameter Description
Correct return:{"access_token":"ACCESS_TOKEN",
1. Appsecret is the key used by the application interface. If leaked, it may lead to application data leakage, application High-risk consequences such as user data leakage; stored on the client, it is very likely to be maliciously stolen (such as decompiling to obtain the Appsecret);2. Access_token is the credential for users to authorize third-party applications to initiate interface calls (equivalent to (in user login state), stored on the client, user data may be leaked after maliciously obtaining access_token, user WeChat related interface functions may be maliciously initiated, etc.; 3. refresh_token is used to authorize third-party applications for users. Long-term credentials are only used to refresh access_token, but if leaked, it will be equivalent to access_token leakage, and the risk is the same as above. It is recommended to put the secret and user data (such as access_token) on the App cloud server, and the cloud transfer interface calls the request. Step 3: Call the interface through access_token After obtaining the access_token, make the interface call with the following prerequisites:1. The access_token is valid and has not expired; 2. The WeChat user has authorized the corresponding interface scope (scope) of the third-party application account. For interface scope (scope), the interfaces that can be called are as follows:
"errcode":40003,"errmsg":"invalid openid "
|
The above is the detailed content of PHP implements WeChat web page login authorization development. For more information, please follow other related articles on the PHP Chinese website!