File 1:index.php
Switch to your own 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 ');
Parameter description:
Parameters
|
Whether you must
|
Description
|
AppID |
Is |
Unique identification of the public number |
Redirect_uri |
Is |
Callback link address that is redirected after authorization, use UrlEncode to process the link |
Response_type |
Is |
return type, please fill in the code |
Scope |
Is |
Application authorization scope, Snsapi_base (not pop-up authorization page, direct jump, can only obtain user OpenID), Snsapi_userinfo (pop-up authorization page, can get nickname, sex, location via OpenID). And, even in the event of not paying attention, can obtain the information as long as the user authorizes. |
State |
Whether |
After redirection, the state parameter is taken, and the developer can fill in the a-za-z0-9 parameter values |
#wechat_redirect |
Is |
This parameter must be taken either directly or on page 302 redirection |
Document II: oauth.php
The code is as follows |
Copy Code |
<?php $code = $_get[' code ']; $state = $_get[' state ']; Switch to your own interface information $appid = ' XXXXX '; $appsecret = ' XXXXX '; if (empty ($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 ' Echo ' <br/>Exit } $access _token_url = ' https://api.weixin.qq.com/sns/oauth2/refresh_token?appid= '. $appid. ' &grant_type=refresh_token&refresh_token= '. $token->refresh_token; Turn into an object $access _token = Json_decode (file_get_contents ($access _token_url)); if (Isset ($access _token->errcode)) { Echo ' Echo ' <br/>Exit } $user _info_url = ' https://api.weixin.qq.com/sns/userinfo?access_token= '. $access _token->access_token. ' &openid= '. $access _token->openid. ' &LANG=ZH_CN '; Turn into an object $user _info = Json_decode (file_get_contents ($user _info_url)); if (Isset ($user _info->errcode)) { Echo ' Echo ' <br/>Exit } Print user Information Echo ' <pre> '; Print_r ($user _info); Echo ' </pre> '; ?> |
Parameters
|
Describe
|
Openid |
Unique identification of the user |
Nickname |
User Nickname |
Sex |
The user's sex, the value of 1 o'clock is male, the value of 2 o'clock is female, the value of 0 o'clock is unknown |
Province |
The province in which the user's personal data is filled |
City |
The city where the ordinary user's personal data fills in |
Country |
Countries, such as China for CN |
Headimgurl |
User Avatar, the last value represents the size of the square head (there are 0, 46, 64, 96, 132 value Optional, 0 represents 640*640 square avatar), the user does not have the Avatar when the item is empty |
Privilege |
User privileged information, JSON array, such as micro-SIM user (chinaunicom) |
Unionid |
The field will only appear if the user binds the public number to the micro-trust Open platform account. See: Get User's personal information (Unionid mechanism) |
Login to this web site licensing development function is done, if you want to get the user's basic information we need to see another example, in the official note that everyone can search by themselves oh.