php WeChat public account development to obtain user WeChat personal information

不言
Release: 2023-03-24 12:08:01
Original
9197 people have browsed it


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!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!