appID=$WxPayConfig->GetAppId(); $WxClient->appsecret=$WxPayConfig->GetAppSecret(); if(!isset($_GET['code'])){ $req = new GetWechatCodeRequest(); $req->setAppId($WxClient->appID); $req->setScope(GetWechatCodeRequest::SNSAPI_USERINFO); //SNSAPI_BASE SNSAPI_USERINFO $url=$req->run(); header('location:'.$url); return ; }else{ $req = new AccessTokenCodeRequest(); $req->setAppId($WxClient->appID); $req->setSecret($WxClient->appsecret); $req->setCode($_GET['code']); $resp=$WxClient->execute($req); if(isset($resp->access_token)){ $UserInfoRequest = new UserInfoRequest(); $UserInfoRequest->setOpenid($resp->openid); $UserInfoRequest->setAccessToken($resp->access_token); $UserInfo=$WxClient->execute($UserInfoRequest); $fileName='/Uploads/headimgurl/'.$UserInfo->openid.'.jpg'; $this->userIconSave($UserInfo->headimgurl, $_SERVER['DOCUMENT_ROOT'].$fileName); $UserInfo->headimgurl=$fileName; //error_log(json_encode($UserInfo), 3, 'openid.log'); return $UserInfo; } return false; } } public function userIconSave($url,$path){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30); $file = curl_exec($ch); curl_close($ch); $resource = fopen($path ,'a'); fwrite($resource, $file); fclose($resource); } } ?>