Home >Backend Development >PHP Tutorial >How to obtain user information based on PHP WeChat webpage
This article mainly provides you with a detailed analysis of the process of using PHP to create a WeChat web page to obtain basic user information, as well as a step-by-step explanation.
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 how to obtain it successfully based on the basics of PHP language.
Necessary conditions:
1) Public account authentication
2) There is a permission interface for web page authorization to obtain basic user information
Note: Recently, a friend said that when applying for a test account on the public platform, it will appear that the user information cannot be obtained. 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 pushes the event after following the message. . 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 two files, index.php and getUserInfo.php
Test first: scope is snsapi_base
1) First follow the public account
2) Generate a URL: http://your domain name/index.php QR code!
3) Use WeChat to scan
and 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.
PHP implements WeChat webpage login authorization development
PHP implements WeChat webpage authorization login
Steps to implement WeChat web page authorized login using ajax (with code)
##
The above is the detailed content of How to obtain user information based on PHP WeChat webpage. For more information, please follow other related articles on the PHP Chinese website!