Home >Backend Development >PHP Tutorial >Split explanation of WeChat distribution platform developed with php+WeChat interface (1) WeChat oauth2 interface
I am an ITmigrant worker. I am not a master or a great god, but I hope to become Lei Feng. Without Shenma's writing, I will just complain. If the explanation is not clear, you can continue to ask questions. I will try my best to answer as time permits.
This article does not provide the entire system source code. It will only open part of the source code and talk about the development experience, ideas, and answers to questions. I hope it will be helpful to novices. As for the experts and experts who have just passed it by or left valuable comments, I would like to thank you here. .
Let’s stop talking and get to the point.
Basics
phpCurrent mainstreamWEBThere is no doubt that development languages needless to say. The WeChat interface mainly uses the WeChat authorized login interface, WeChatJSSDKsharing interface, and WeChat payment interface. We must complain about the series of WeChat interfaces that are full of pitfalls. The distribution system is a marketing model that will be explained later. First, let’s talk about the first step of WeChat login (oauth2Third-party authorization interface), which I personally think is more important. It has the advantages of high user experience, high security, and lays a convenient foundation for the subsequent distribution system. The disadvantages are only Accessing in WeChat’s built-in browser is inconvenient for publicity and promotion in other ways than WeChat. You can only use QR codes (there are many third-party tools online to generate QR codes in the form of links, and you can also use programs to achieve this. If you are interested, you can leave a message. ).
First take a look at the legendary WeChatAPIinterface document, the official website document addresshttp://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b 468d75.html
You must have a certified service account to use this interface. If you don’t have one, you can apply for a test account to experience it but it cannot be promoted.
Instructions on the webpage authorization callback domain name
The document is relatively clear. When not using a third-party platform, you need to configure the path through which the project needs to obtain WeChat user information. Either first- or second-level domain names are acceptable. This one is relatively clear
Looking down, I didn’t understand it the first time, and I didn’t understand it the second time. . . . . . The project started to be developed a year ago. The documentation at that time was even simpler than it is now, and there were very few reference materials on the Internet. There were no official examples. Fortunately, I finally found some clues from an article and it was done. I’m going to complain about it
Other instructions
First introduce a few parameters appid and appsecretYou can find these two parameters used in many interface calls in the WeChat public platform.
The most important parameter is also the purpose of calling the authorization interface, openid, which is the only identification of a WeChat ID corresponding to the public platform. Once it is obtained, it can be considered that the user has logged in with WeChat, and all subsequent business processes and database records It's all built around it.
Let’s talk about how to obtain openid and basic user information such as avatar, nickname, city, gender, etc. This is a rather painful process. There are so many steps to fool us, right? I'll give you a reason for safety reasons. Sorry for whining again.
Get the code in three steps
1, what is a code? ? ? I don’t know, but I need to get it to proceed to the next step
Interface address:https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&resp/span> appid has just been improved and can be found on the public platform. redirect_uri is the key point. It is when the user sends a request to the WeChat server through the above interface address. The WeChat server transmits it through GET A parameter code returns to redirect_uri, just use $_GET to accept it. scope is also an important parameter. It has two types snsapi_base and snsapi_userinfo. Let’s talk about the difference. If you only want to achieve WeChat login, you only need openid then use it. snsapi_base,The advantage is that the user does not know that you have captured his openid when visiting. In addition to grabbing openid, using snsapi_userinfo can also capture a lot more WeChat user information. Now many WeChat applications require avatars and nicknames so you can use snsapi_userinfo directly. The disadvantages are A page showing whether WeChat authorization is allowed will pop up first, and the user must agree to the next step before proceeding. As shown in the picture:
2. openidand access_tokenThrough the and appid and appsecretinterface: https://api.weixin.qq .com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_codereturns data in format 3 ... through and access_tokeninterface https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID &lang=zh_CN returns data in formatparameters default returns English data when not written Example code Snippet a .php $url='http://www.xxx.com/b.php'; $url=urlencode($url); $href="https:// open.weixin.qq.com/connect/oauth2/authorize?appid=xxx&redirect_uri=$url&resp/span> @header("location:$href"); b.php $code=$_GET['code']; $url='https://api.weixin.qq.com/sns/oauth2/access_token?appid=xxx&secret=xxx&code='.$code. '&grant_type=authorization_code'; $js/span> $jsonstr = json_decode($jsonstr,true); $access_token= $jsonstr['access_token']; $openid =$jsonstr['openid']; $userurl='https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid='.$openid; $userinfo=file_get_contents($userurl); $userinfo = json_decode($userinfo,true); Okay, it’s barely implemented. . . . . . . . . . However, after a period of operation, the information capture is not stable, and some users will not get their avatars and nicknames. I have been pondering for a long time and don’t know where the problem is ? ? ? ? ? Oh, there is a problem with the function . Although it can be achieved by using file_get_contents, the performance effect is extremely unstable. Later, I switched to curl, which is much more stable but does not work. Reaching 100%catch is caused by many factors, but it is basically within the acceptable range.
Time is limited today. There is another way to define the distribution system and obtain WeChat user avatar nickname and other information without popping up the authorization interface. unionidNeeded to be used when developing multiple public platforms and to achieve persistent login status. cookie, as well as the shopping cart implementation of the mall part of the distribution system client will be explained in the second time. Attached is a QR code for the system access path. You can take a look at it first and it can only be accessed through WeChat. You can also ask questions about the system architecture, front-end JSscripts and the implementation of a series of functional modules. I will change the content of the next update based on what most people want Copyright Statement: This article This is an original article by the blogger and may not be reproduced without the blogger's permission.
The above introduces the split explanation of the WeChat distribution platform developed by PHP + WeChat interface (1) WeChat oauth2 interface, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.