How to get user information based on PHP WeChat web page

Source: Internet
Author: User
Tags php language openid urlencode
This article mainly gives you a detailed analysis of the use of PHP to create a Web page to obtain the user's basic information process, as well as steps to explain.





A lot of users in the development of the page, need to get basic information of the user, such as country, province, city, nickname, etc., we then based on the PHP language based on a detailed analysis of how to successfully obtain.



Necessary:



1) Public Number certification



2) a permission interface to access user's basic information with Web page authorization



Note: Recently, a friend said: in the public platform to apply for the test number, there will be no access to user information. It's normal to change to a certified public account!



If you are experiencing this problem, try testing it in a Certified public account! Thank you for your support!



Fill in the domain name of the authorization callback page



Sign in to the public platform--Developer Center--Interface permissions table



Find the Web page authorization to get the user basic information and then modify-fill your domain name as follows:






Save it!



A description of the difference between the two scopes of the Web page authorization (official)



1, the snsapi_base for the scope of the Web page authorization, is used to obtain access to the page of the user OpenID, and is silent authorization and automatically jump to the callback page. User-perceived is directly into the callback page (often the business page)



2, to Snsapi_userinfo for the scope of the Web page authorization, is used to obtain the user's basic information. However, this authorization requires the user to agree manually, and because the user has consented to, so there is no need to pay attention, you can obtain the user's basic information after authorization.



3, the user management interface in the "access to the user's basic information interface", the user and the public number generated after the message interaction or concern after the event push, to be based on user OpenID to obtain basic user information. This interface, including other interfaces, is required for the user (that is, OpenID) to follow the public number before the call succeeds.



Since the scope has two modes, the following is a separate explanation:



Scope is snsapi_base so the user must be concerned about the public number to get information



Build your own two files: index.php and getuserinfo.php



Code instance



index.php as follows:





//scope=snsapi_base instance
$appid='Your AppId';
$redirect_uri = urlencode ('http://your domain name/getUserInfo.php' );
$url ="https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appid&redirect_uri=$redirect_uri&response_type=code&scope=snsapi_base&state=1#wechat_redirect";
header("Location:".$url);





getuserinfo.php as follows:





$appid = "Your AppId";
$secret = "Your AppSecret";
$code = $_GET["code"];
//The first step: take the global access_token
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$secret";
$token = getJson($url);
//The second step: get the openid
$oauth2Url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$secret&code=$code&grant_type=authorization_code";
$oauth2 = getJson($oauth2Url);
 
//The third step: query user information based on global access_token and openid
$access_token = $token["access_token"];
$openid = $oauth2['openid'];
$get_user_info_url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=$access_token&openid=$openid&lang=zh_CN";
$userinfo = getJson($get_user_info_url);
//Print user information
print_r($userinfo);
function getJson($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
return json_decode($output, true);
}





Scope for Snsapi_userinfo users do not have to pay attention to the public number, but also access to information, but there will be an interface for users to click to confirm! Equivalent to a login authorization!



Code instance



index.php as follows:





//scope=snsapi_userinfo instance
$appid='Your AppId';
$redirect_uri = urlencode ('http://your domain name/getUserInfo.php' );
$url ="https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appid&redirect_uri=$redirect_uri&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect";
header("Location:".$url);





getuserinfo.php as follows:





$appid = "Your AppId";
$secret = "Your AppSecret";
$code = $_GET["code"];
//The first step: get openid
$oauth2Url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$secret&code=$code&grant_type=authorization_code";
$oauth2 = getJson($oauth2Url);
//The second step: query user information based on global access_token and openid
$access_token = $oauth2["access_token"];
$openid = $oauth2['openid'];
$get_user_info_url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=$access_token&openid=$openid&lang=zh_CN";
$userinfo = getJson($get_user_info_url);
//Print user information
print_r($userinfo);
function getJson($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
return json_decode($output, true);
} 





Test steps:



After creating index.php and getuserinfo.php two files



Test first: Scope is snsapi_base



1) Focus on public accounts first



2) Add URL:/HTTP//your domain name/index.php generate a two-dimensional code!



3) Sweep a sweep



Re-test: scope is Snsapi_userinfo



1) Replacement code



2) Cancel the attention to the current public number.



3) then sweep, just the QR code you generated.






Related recommendations:



PHP implementation of Web Landing authorization development



PHP Implementation Page Authorization Login



Implementation of the page authorization login with Ajax (code attached)
















Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.