话不多说,直接上代码:
//获取令牌 public function getAccessToken(){ //指定保存文件位置 if(!is_dir(&#39;./access_token/&#39;)){ mkdir(iconv("GBK","UTF-8",&#39;./access_token/&#39;),0777,true); } $file = &#39;./access_token/token&#39;; if(file_exists($file)){ $cOntent= file_get_contents($file); $cOnt= json_decode($content); if( (time()-filemtime($file)) <$cont->expires_in){ //当前时间-文件创建时间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; }
就可以在用户不知不觉的时候,拿到如下信息: