


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!

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Atom editor mac version download
The most popular open source editor

SublimeText3 Linux new version
SublimeText3 Linux latest version
