Home Backend Development PHP Tutorial php WeChat public account development to obtain user WeChat personal information

php WeChat public account development to obtain user WeChat personal information

Apr 19, 2018 pm 02:35 PM
php personal user


Many developers need to obtain the user's avatar when developing a WeChat official account. If it is a personal WeChat account, you need to apply for a "public platform test account" to obtain a WeChat official account. All interface permissions;

First, you need to obtain the access_token sent by WeChat, and then use the openid and access_token to obtain the user message in json format sent by the WeChat server. Finally, use josn_decode to convert it to obtain the user's details. News;

Before writing the code, you can test the interface operation as shown below:

First enter the WeChat public platform, as shown below:


Click on Developer Tools, and then click on Online Interface Debugging, as shown in the figure below, to enter the interface selection interface


Click Then enter the page below and select in the interface type: User management, select in the interface list: Get user profile information interface/user/info;

Copy after login

Then check the WeChat development documentation to obtain access_token and use the appid and appsecret as shown below




################# qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret" URL, where $appid is the appid string in the above picture, $appsecret is your developer password, replace the two with strings After the variable (be careful to remove the quotation marks), click the enter key to obtain the access_token and expires_in data sent by the WeChat server. Copy the obtained access_token string to the interface debugging page. Openid is the test account and the only ID after following the platform is encrypted. Yes, please see the previous article for the method of obtaining it, or you can also use Baidu. Enter the two parameters into the corresponding input boxes on the interface page (the green word "passed verification" will be displayed after the input is completed), and then click to detect the problem; ## #######The obtained information is as shown below: ##################This means that you have successfully obtained it. Let’s add the code to obtain the access_token and personal information; ###
public function getaccss_token(){
		$table = 'Access_token';
		$useraccess_token = Db::table('Access_token')->select();
		$appid = "你的appid";
		$appsecret = "你的appsecret";
		$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret";
		//判断是不是第一次获取access_token		
		if(!count($useraccess_token)){			
			$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);
			$jsoninfo = json_decode($output, true);
			$access_token = $jsoninfo["access_token"];			
			$data=['access_token'=>$access_token,'expires_time'=>($jsoninfo['expires_in']+time()-200)];
			Db::table($table)->insert($data);
			return $access_token;
	}else if($useraccess_token[0][&#39;expires_time&#39;]<time()){//判断是否过期
		
			$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);
			$jsoninfo = json_decode($output, true);//转换格式
			$access_token = $jsoninfo["access_token"];			
			$data=[&#39;access_token&#39;=>$access_token,&#39;expires_time&#39;=>($jsoninfo[&#39;expires_in&#39;]+time()-200)];
			Db::table($table)->where(&#39;expires_time&#39;,$useraccess_token[0][&#39;expires_time&#39;])->update($data);//更新数据库;
			return $access_token;
	}else{	
		$access_token = $useraccess_token[0][&#39;access_token&#39;];
		return $access_token;
		
	}
	}
Copy after login
###Use the curl function to intercept the message sent by WeChat. Since the access_token obtained is only valid for 7200s, which is two hours, so every time the user makes an entry operation on the official account, it must be checked to see if it is It has expired. If it expires, it needs to be obtained again. The several if judgments in the picture above are used to judge. I save the access_token and time in the database, so that it can be taken out for detection every time. Of course, you can also use it from the cache. Call or put it into a file and call it from the file; ######After obtaining the access_token, obtain the user's personal information through access_token and openid, as shown below: ###
public function userinfo($openid){
		$access_token = $this->getaccss_token();
		//获取用户信息地址		
		$urlid = &#39;https://api.weixin.qq.com/cgi-bin/user/info?access_token=&#39;.$access_token.&#39;&openid=&#39;.$openid.&#39;&lang=zh_CN&#39;;
		$curl = curl_init(); // 启动一个CURL会话
		curl_setopt($curl, CURLOPT_URL, $urlid);
		curl_setopt($curl, CURLOPT_HEADER, 0);
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
		//curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, true);  // 从证书中检查SSL加密算法是否存在
		$tmpInfo = curl_exec($curl);     //返回api的json对象
		//关闭URL请求
		curl_close($curl);
                $userinfo = json_decode($tmpInfo,true);			
		return $userinfo;
	}
Copy after login
### is also related to the curl function Operate to intercept the information and convert the format. The last userinfo is the user's personal information array. You only need to call it; ######The above is the operation to obtain the user's personal information through access_token~! ######Related recommendations: #########php WeChat public account development, obtain user avatar, and download######

The above is the detailed content of php WeChat public account development to obtain user WeChat personal information. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1272
29
C# Tutorial
1252
24
PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

See all articles