Home  >  Article  >  Backend Development  >  How to obtain user information in PHP WeChat webpage

How to obtain user information in PHP WeChat webpage

php中世界最好的语言
php中世界最好的语言Original
2017-12-20 16:43:401772browse

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 authorization to obtain the user's basic information and then modify it-->Fill in your domain name. As follows:

Save it!

Two things about web page authorization Explanation of the differences between the two 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 jumps 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 manual consent from the user, and since the user has agreed, the basic information of the user can be obtained after authorization without paying attention.

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 The .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.


I believe you have mastered the method after reading these cases. For more exciting information, please pay attention to php Chinese website Other related articles!

Related reading:

How does PHP solve the problem of large website traffic and high concurrency

What is the common syntax of AJAX

AJAX principles and CORS cross-domain methods

The above is the detailed content of How to obtain user information in PHP WeChat webpage. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn