Php public platform webpage logon authorization example, php example
This article describes how to authorize the php public platform to log on to the webpage. We will share this with you for your reference. The details are as follows:
It is actually very simple to implement webpage login authorization development on the public platform. Because of the official reference program, the following small series will show an example of webmasters performing webpage login authorization based on official reference, let's take a look.
File 1: index. php
// Change your interface information $ appid = 'xxxxx'; header ('location: https://open.weixin.qq.com/connect/oauth2/authorize? Appid = '. $ appid.' & redirect_uri = 127.0.0.1/oauth. php & response_type = code & scope = snsapi_userinfo & state = 123 & connect_redirect = 1 # wechat_redirect ');
Parameters |
Required? |
Description |
Appid |
Yes |
Unique public ID |
Redirect_uri |
Yes |
URL of the redirection callback link after authorization. Use urlencode to process the link. |
Response_type |
Yes |
Return type. Enter the code |
Scope |
Yes |
Application Authorization scope, snsapi_base (the authorization page is not displayed, Jump directly, only the user's openid can be obtained), snsapi_userinfo (the authorization page is displayed, and the nickname, gender, and location can be obtained through openid. In addition, users can obtain information even if they do not pay attention to it) |
State |
No |
With the state parameter, the developer can fill in the parameter value of the a-zA-Z0-9 |
# Wechat_redirect |
Yes |
This parameter must be included when you open the page directly or perform page 302 redirection. |
File 2: oauth. php, the Code is as follows:
<? Php $ code = $ _ GET ['code']; $ state = $ _ GET ['state']; // change the information of your own interface $ appid = 'xxxxx '; $ appsecret = 'xxxxx'; if (emptyempty ($ code) $ this-> error ('authorization failed'); $ token_url = 'https: // api.weixin.qq.com/sns/oauth2/access_token? Appid = '. $ appid. '& secret = '. $ appsecret. '& code = '. $ code. '& grant_type = authorization_code'; $ token = json_decode (file_get_contents ($ token_url); if (isset ($ token-> errcode) {echo '
Parameters |
Description |
Openid |
Unique User ID |
Nickname |
User nickname |
Sex |
Gender of the user. If the value is 1, the user is male. If the value is 2, the user is female. If the value is 0, the user is unknown. |
Province |
Province of the user's personal data |
City |
City for normal user Personal Data |
Country |
Country, such as CN |
Headimgurl |
User profile picture. The last value indicates the size of the square profile picture (optional values include 0, 46, 64, 96, and 132, and 0 indicates the size of the 640x640 square profile picture ), this item is blank when the user does not have an avatar. |
Privilege |
User Privilege information, which is a json array. For example, the Waka user is (chinaunicom) |
Unionid |
This field appears only when you bind a public account to an open platform account. For details, see obtain user personal information (UnionID mechanism) |
This web page is used up to log on to the authorized development function. If you want to obtain basic user information, we need to look at another example. If you have instructions on the official website, you can search by yourself.