我正在开发自动登录网站的php curl。但是,我有问题,该站点需要将_csrf_token发送到服务器。 我的代码如下。首先,程序称为addLoginData($ users),然后是getToken()和getHTTPContent()。
我不知道为什么代码不起作用。
public function addLoginData($users)
{
foreach($users as $user)
{
$login_arr = array(
/*'commit' => 'Login',
'nickname' => $user['username'],
'password' => $user['password'],
'save_cookie' => '1'*/
'_csrf_token' => $this->getToken(),
'action' => 'login',
'commit' => 'Einloggen',
'invisibility' => 0,
'nickname' => $user['username'],
'online_status' => 0,
'password' => $user['password'],
'referer' => '@homepage_guest',
'remember_me' => 1
);
array_push($this->loginArr, $login_arr);
}
}
public function getToken()
{
$content = $this->getHTTPContent($this->loginURL,$this->rootDomain);
$token = '';
if(!empty($content)) {
$html = str_get_html($content);
if($html->find("input[name=_csrf_token]",0)) {
foreach($html->find("input[name=_csrf_token]") as $span) {
$token = $span->value;
}
}
}
return $token;
}
protected function getHTTPContent($url, $referer, $cookiePath=null, $postContent=null, $get_info = FALSE, $header = null)
{
$ch = curl_init();
if($this->command['proxy_type'] != 3 && !empty($this->proxy_ip) && !empty($this->proxy_port) && !empty($this->proxy_type)){
curl_setopt($ch, CURLOPT_PROXY, $this->proxy_ip);
curl_setopt($ch, CURLOPT_PROXYPORT, $this->proxy_port);
curl_setopt($ch, CURLOPT_PROXYTYPE, $this->proxy_type);
}
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_REFERER, $referer);
curl_setopt($ch, CURLOPT_USERAGENT, $this->userAgent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
// curl_setopt($ch, CURLOP T_CAINFO, dirname(__FILE__)."/cacert.pem");
$this->savelog("=>".$ch);
print_r($ch);
if($header !== null) {
curl_setopt($ch,CURLOPT_HTTPHEADER, $header);
}
if($cookiePath !== null)
{
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiePath);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiePath);
}
if($postContent !== null || $this->nullPost == 1)
{
curl_setopt($ch, CURLOPT_POST, 1);
if($this->nullPost == 0) {
curl_setopt($ch, CURLOPT_POSTFIELDS, (($this->_special_post == 1) ? $postContent : http_build_query($postContent)));
$this->_special_post = 0;
if($this->_special_post == 1){
echo 'Special Search';
}
var_dump($postContent);
}
$this->nullPost = 0;
}
$content = curl_exec($ch);
$header = curl_getinfo($ch);
curl_close($ch);
echo '<p>URL : ', $url,'</p>';
echo '<p><textarea style="width:600px; height:400px;">',$content,'</textarea></p>';
if(empty($content)) {
$this->savelog('No Response from url : '.$url.' / Proxy : '.$this->proxy_ip.':'.$this->proxy_port); botutil::setNoResponse($this->commandID, TRUE, $this);
} else {
botutil::setNoResponse($this->commandID, FALSE, $this);
}
if($get_info === TRUE) {
return array(
'header' => $header,
'content' => $content
);
} else {
return $content;
}
}
答案 0 :(得分:0)
@Tufan Barış Yıldırım, the login function is below.
public function login()
{
$this->userAgent = botutil::getAgentString();
$this->currentUser = 0;
$username = $this->loginArr[$this->currentUser][$this->usernameField];
$cookiePath = $this->getCookiePath($username);
$this->user_name = $username;
if(!($this->isLoggedIn($username)))
{
$this->savelog("This profile: ".$username." does not log in.");
// count try to login
for($count_login=1; $count_login<=$this->loginRetry; $count_login++)
{
if($this->command["proxy_type"] == 1){
if($this->tor_new_identity($this->proxy_ip,$this->proxy_control_port,'bot')){
$this->savelog("New Tor Identity request completed.");
}else{
$this->savelog("New Tor Identity request failed.");
}
}
$this->savelog("Logging in.");
// Log
$content = $this->getHTTPContent($this->loginActionURL, $this->rootDomain, $cookiePath, $this->loginArr[$this->currentUser]);
if(!empty($content)) {
file_put_contents("login/".$username."-".date("YmdHis").".html",$content);
}
if(empty($content))
{
$this->savelog("No response from server.");
$this->loginRetry++;
}
else if(!($this->isLoggedIn($username)))
{
$this->savelog("Log in failed with profile: ".$username);
$this->savelog("Log in failed $count_login times.");
if($count_login>($this->loginRetry-1))
{
$this->savelog("User ".$username." tried to login ".$count_login." times. This username would be deleted.");
DBConnect::execute_q("UPDATE user_profiles SET status='false' WHERE site_id=".$this->siteID." AND username='".$this->loginArr[$this->currentUser]['data']['User'][$this->usernameField]."'");
$this->command['profile_banned'] = TRUE;
return false;
}
else
{
$sleep_time = 120; // 2 mins
$this->_session_id = NULL;
$this->savelog("Sleep after log in failed for ". $this->secondToTextTime($sleep_time));
$this->sleep($sleep_time);
}
} else {
botutil::profileCount($this->getSiteID(), $username);
return true;
}
}
}
else
{
return true;
}
}
public function logout()
{
$username = $this->loginArr[$this->currentUser][$this->usernameField];
$cookiePath = $this->getCookiePath($username);
$this->savelog("Logging out.");
$content = $this->getHTTPContent($this->logoutURL. time(), $this->rootDomain, $cookiePath);
return true;
}