PHP micro-Credit development Get user information
The approximate algorithm for obtaining user information is
Users are authorized to log on to third party websites,
Emphasis: Scope Parameters:
Snsapi_basic Silent Login, do not require user authorization, can only obtain OpenID;
Snsapi_userinfo, require user to click Authorization, can obtain OpenID and all user information;
The first step: Get the user's code value first;
The second step: according to the code value to get Access_token, each request value is different, if not used, every five minutes update;
The third step: according to Access_token to obtain user information;
1. Get Code implementation:
getcode.php
if (Isset ($_session[' user ')) {
Print_r ($_session[' user '));
Exit;
}
$appid = ' wx1d7c6fcd6131143b3 ';
$redirect _url= "http://www.antfortune.vip/callback.php";
$scope = ' snsapi_userinfo '; the way of acquiring;
$url = ' https://open.weixin.qq.com/connect/oauth2/authorize?appid= '. $appid. ' &redirect_uri= '. UrlEncode ($redirect _url). ' &response_type=code&scope= '. $scope. ' &state=123#wechat_redirect ';
Header ("Location:". $url);
2, according to the code to obtain Access_token and OpenID
getopenid.php
<?php
//Get user OpenID
$appid = "Your AppID";
$appsecret = "Your Appsecret";
$code =$_get[' Code '];
function Getopenid ($appid, $appsecret, $code) {
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid= ". $appid." &secret= ".
$appsecret. " &code= ". $code." &grant_type=authorization_code ";
$weixin =file_get_contents ($url);//through code in exchange for Web page authorization Access_token
$jsondecode =json_decode ($weixin); Encode JSON-formatted strings
$array = Get_object_vars ($jsondecode);//convert array
$openid = $array [' OpenID '];//output OpenID return
$openid;
}
Echo Getopenid ($appid, $appsecret, $code);
Thank you for reading, I hope to help you, thank you for your support for this site!