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:
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 = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$this->appid.'&secret='.$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=''){
if(!$openid) return false;
$access_token = $this->getAccessToken();
$urlStr = 'https://api.weixin.qq.com/cgi-bin/user/info?access_token=%s&openid=%s&lang=zh_CN';
$url = sprintf($urlStr,$access_token,$openid);
$result = json_decode($this->_request($url),true);
return $result;
}
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!