Home  >  Article  >  Backend Development  >  Example analysis of obtaining user information based on PHP WeChat web page

Example analysis of obtaining user information based on PHP WeChat web page

coldplay.xixi
coldplay.xixiforward
2020-08-05 17:18:052163browse

Example analysis of obtaining user information based on PHP WeChat web page

When many users develop WeChat web pages, they need to obtain the user’s basic information, such as country, province, city, nickname, etc. Next, we will analyze in detail based on the PHP language foundation how to Obtained successfully.

Related video recommendations: PHP programming from entry to proficiency

Necessary conditions:

1) Public account Authentication

2) There is a web page authorization interface for obtaining basic user information

Note: Recently, a friend said: The test account applied for on the public platform will appear to be unavailable. to user information. It will be normal if you switch to a certified public account!

If you also encounter this problem, you can try testing it in a certified public account! thanks for your support!

Fill in the domain name of the authorization callback page

Log in to the public platform-->Developer Center-->Interface permission table

Find the web page to authorize and obtain basic user information and modify it-->Fill in your domain name. As follows:

Save it!

About the web page Explanation of the difference between the two authorization scopes (official)

1. Web page authorization initiated with snsapi_base as the scope is used to obtain the openid of the user who enters the page, and is silently authorized and automatically jumped. Go to the callback page. What the user perceives is that they directly enter the callback page (often a business page)

2. The web page authorization initiated with snsapi_userinfo as the scope is used to obtain the user's basic information. However, this kind of authorization requires the user to manually agree, and since the user has agreed, there is no need to pay attention, and the user's basic information can be obtained after authorization.

3. The "Obtain User Basic Information Interface" in the user management interface can obtain the user's basic information based on the user's OpenID only after the user interacts with the official account or after the event is pushed after the user interacts with the official account. . This interface, including other WeChat interfaces, requires the user (i.e. openid) to follow the official account before it can be called successfully.

Because scope has two modes, we will explain them separately below:

scope is snsapi_base, then users must follow the official account to obtain information

First create two files yourself: index.php and getUserInfo.php

Code example

index.php is as follows:

//scope=snsapi_base 实例
$appid='你的AppId';
$redirect_uri = urlencode ( 'http://你的域名/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 is as follows:

$appid = "你的AppId";
$secret = "你的AppSecret";
$code = $_GET["code"];
//第一步:取全局access_token
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$secret";
$token = getJson($url);
//第二步:取得openid
$oauth2Url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$secret&code=$code&grant_type=authorization_code";
$oauth2 = getJson($oauth2Url);
 
//第三步:根据全局access_token和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_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 is snsapi_userinfo. Users can get information without following the official account, but there will be an interface for users to click to confirm! It’s equivalent to a login authorization!

Code example

index.php is as follows:

//scope=snsapi_userinfo实例
$appid='你的AppId';
$redirect_uri = urlencode ( 'http://你的域名/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 is as follows:

$appid = "你的AppId";
$secret = "你的AppSecret";
$code = $_GET["code"];
//第一步:取得openid
$oauth2Url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$secret&code=$code&grant_type=authorization_code";
$oauth2 = getJson($oauth2Url);
//第二步:根据全局access_token和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_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 the two files index.php and getUserInfo.php

Test first: the scope is snsapi_base

1) Follow the public account first

2) Generate a QR code with the URL: http://your domain name/index.php!

3) Scan it with WeChat

Test again: scope is snsapi_userinfo

1) Replace the code

2) Unfollow the current public account.

3 ) Then use WeChat to scan the QR code you just generated.

Related learning recommendations: php programming (video)

The above is the detailed content of Example analysis of obtaining user information based on PHP WeChat web page. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:jb51.net. If there is any infringement, please contact admin@php.cn delete