How to obtain user basic information on WeChat webpage

小云云
Release: 2018-03-20 14:55:14
Original
4869 people have browsed it

This article mainly shares with you the method of obtaining user basic information on WeChat webpage. I have written an article beforeWeChat webpage authorizes to obtain user basic informationWhen talking about web development, sometimes it is necessary to obtain Basic personal information such as the user's nickname, avatar, etc. can be obtained through authorization on the web page. After the user agrees, it can be obtained, as shown in the figure:


##However,

This adds one more step. Some users may see the extra step and click back., thus losing a valuable user. It is very difficult to It's a pity, so today we will use another way to obtain user information, which is to obtain user basic information through OpenID. This method is much simpler. Without the user knowing it, we have obtained his personal information, and the information obtained in this way is more than what was authorized. Many, for example, you can also know whether the user is paying attention and how long he has been paying attention. Preliminary preparation: Obtain the user's openid. This was discussed in the previous article. For details, see: WeChat official account obtains the user's openid


Without further ado, let’s go directly to the code:

//获取令牌
public function getAccessToken(){
	
	//指定保存文件位置
	if(!is_dir('./access_token/')){
		mkdir(iconv("GBK","UTF-8",'./access_token/'),0777,true); 
	}
	$file = './access_token/token';
	if(file_exists($file)){
		$content = file_get_contents($file);
		$cont = json_decode($content);
		if( (time()-filemtime($file)) < $cont->expires_in){   //当前时间-文件创建时间<token过期时间
			return $cont->access_token;
		}
	}
	
	$curl = &#39;https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=&#39;.$this->appid.&#39;&secret=&#39;.$this->appsecret;
	$content = $this->_request($curl);
	file_put_contents($file,$content);
	$cont = json_decode($content);
	return $cont->access_token;

}

/**
 * 通过openid拉取用户信息
 * @param  string $openid [description]
 * @return [type]         [description]
 */
public function getUserInfo($openid=&#39;&#39;){
	if(!$openid) return false;
	$access_token = $this->getAccessToken();
	$urlStr = &#39;https://api.weixin.qq.com/cgi-bin/user/info?access_token=%s&openid=%s&lang=zh_CN&#39;;
	$url = sprintf($urlStr,$access_token,$openid);
	$result = json_decode($this->_request($url),true);
	return $result;
}
Copy after login
Then the user can get the following information without the user knowing it:

Related recommendations:

Develop WeChat public platform to obtain basic user information

WeChat public platform development Obtain basic user information

WeChat public account development web page authorization to obtain basic user information

The above is the detailed content of How to obtain user basic information on WeChat webpage. 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!