【问题标题】:"Session expired" and "Please enable cookies" warnings when POSTing website form through cURL通过 cURL 发布网站表单时出现“会话已过期”和“请启用 cookie”警告
【发布时间】:2011-01-24 08:59:59
【问题描述】:

我一直在开发一个能够登录 AWeber.com 并执行大量数据导入的程序。该脚本使用 PHP cURL 库及其 CookieJar 设置来欺骗普通用户的浏览器。

该脚本运行良好,允许登录和更改列表,但在发布表单数据(在 submitData 函数中)时,脚本总是失败。每次网站都会输出一个网页,表明会话已经过期,并要求“用户”重新登录。该页面还请求“用户”在其浏览器中启用 cookie。

在过去的几天里,我一直在诊断问题,这让我完全难过。 CURLOPT_VERBOSE 设置表示 cURL 正在将 cookie 传递给网站,cookiejar 文件包含 cookie,并且已从原因因素中消除了其他因素,例如 Referer 和 Javascript 要求。

对于为什么会发生这种情况以及问题的解决方案,我将不胜感激。导致错误的类和代码如下所示。代码在发生错误的地方被标记。

<?php
class AWeber {
  private $cj;

  public function __construct() {
    $this->cj = tempnam('/tmp', 'mlcookies_');
  }

  private function postQuery( $url, $array ) {
    $cu = curl_init();
    curl_setopt( $cu, CURLOPT_URL, $url );
    curl_setopt( $cu, CURLOPT_POST, true );
    curl_setopt( $cu, CURLOPT_POSTFIELDS, $array );
    curl_setopt( $cu, CURLOPT_COOKIEJAR, $this->cj );
    curl_setopt( $cu, CURLOPT_COOKIEFILE, $this->cj );
    curl_setopt( $cu, CURLOPT_RETURNTRANSFER, true );
    curl_setopt( $cu, CURLOPT_FOLLOWLOCATION, true );
    curl_setopt( $cu, CURLOPT_HEADER, true );
    curl_setopt( $cu, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0)' );

    $return = curl_exec($cu);
    echo $return;
    curl_close($cu);
    return $return;
  }

  private function getQuery( $url ) {
    $cu = curl_init();
    curl_setopt( $cu, CURLOPT_COOKIEJAR, $this->cj );
    curl_setopt( $cu, CURLOPT_COOKIEFILE, $this->cj );
    curl_setopt( $cu, CURLOPT_URL, $url );
    curl_setopt( $cu, CURLOPT_RETURNTRANSFER, true );
    curl_setopt( $cu, CURLOPT_FOLLOWLOCATION, true );
    curl_setopt( $cu, CURLOPT_HEADER, true );
    curl_setopt( $cu, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0)' );

    $return = curl_exec($cu);
    curl_close($cu);
    echo $return;
    return $return;
  }

  public function login( $user, $pass ) {
    $this->getQuery( "https://www.aweber.com/login.htm" ); // Get page cookie checks
    $query = array(
      '_method' => 'POST',
      'data[Account][username]' => $user,
      'data[Account][password]' => $pass,
      'data[Account][remember_login]' => '1'
    );
    $return = $this->postQuery( "https://www.aweber.com/login.htm", $query );
    $this->getQuery( "https://www.aweber.com/users/" );
    if ( !$return ) return false;  
    if ( strpos($return, '<div class="aw-status-headline">Error</div>') === false ) {
      return true;
    } else {
      return false;
    }
  }

  public function setList( $list ) {
    $return = $this->getQuery( "https://www.aweber.com/users/lists/change/" . $list );
    if ( !$return ) return false;
    if ( strpos($return, '<option selected="selected" id="listSelectionActiveOption" value="' . $list . '">' ) === false ) {
      return false;
    } else {
      return true;
    }
  }

  public function submitData( $text, $note ) {
    $query = array(   
      'upload_file' => '1',
      'data[ImportWizard][leads]' => $text,
      'data[ImportWizard][delimiter]' => 'TAB',
      'data[ImportWizard][customer_note]' => $note,
      'data[ImportWizard][use_automation]' => '1',
      'cmd' => 'Next',
    );
    $return = $this->postQuery( "https://www.aweber.com/users/lead_imports", $query );
    echo $return;
    if ( !$return || strpos($return, '<h1>Step 2</h1>') === false ) return false;

    $query = array(
      'columnArray' => '',
      'columnArray' => '',
      'data[ImportWizard][column0]' => 'name',
      'data[ImportWizard][column1]' => 'email',
      'cmd' => 'Save',
      );
    $return = $this->postQuery( "https://www.aweber.com/users/lead_imports", $query );
    if ( !$return || strpos($return, '<div class="aw-status-headline">Import Queued</div>') === false ) return false;
    return true;
  }

}

$aw = new AWeber(); // Create new AWeber interface class instance
$aw->login( $aUser, $aPass ) or trigger_error('Login failed', E_USER_ERROR); // Login
$aw->setList( 'list1' ) or trigger_error('List change 1 failed', E_USER_ERROR); // Change mailing list to 'list1'

// *** CODE WILL FAIL HERE WITH "Data submit 1 failed" ERROR ***
$aw->submitData( "Test\tTesterrr\nTest2\tTesterrr2\nTest3\tTesterrr3\n", "Testing Testing Testing Testing Testing Testing Testing" ) or trigger_error('Data submit 1 failed', E_USER_ERROR); // Submit data
$aw->setList( 'list2' ) or trigger_error('List change 2 failed', E_USER_ERROR); // Change mailing list to 'list2'
?>

【问题讨论】:

  • 对 postQuery 的哪个调用在 submitData 函数中失败,第一次还是第二次?

标签: php session cookies post curl


【解决方案1】:

这可能是因为您在会话期间的调用之间使用 curl_close 关闭了 curl 句柄。只能在会话使用完成后关闭。

【讨论】:

    【解决方案2】:

    我认为 Turnkey 是正确的。

    这可能是因为您正在关闭 带有 curl_close 的卷曲句柄 在会话期间的呼叫之间。它 应该只在之后关闭 会话使用已完成。

    您不能打开两个不同的 curl 会话。 尝试在一个会话中完成所有操作,然后关闭会话。

    尝试在类的析构函数中添加 curl_close()。将 curl 会话存储在变量中。并使用 $this->curl_session 访问它。 这是一个例子

    <?php
        class AWeber {
        $curl_session;
        $cj;
        function __construct() {
            $this->curl_session = curl_init();
        }
        function __destruct() {
            if($this->curl_session) {
                curl_close($this->curl_session);
            }
        }
        function doWhatever() {
                curl_setopt( $this->curl_session, CURLOPT_COOKIEJAR, $this->cj );
                curl_setopt( $this->curl_session, CURLOPT_COOKIEFILE, $this->cj );
                curl_setopt( $this->curl_session, CURLOPT_URL, $url );
                curl_setopt( $this->curl_session, CURLOPT_RETURNTRANSFER, true );
                curl_setopt( $this->curl_session, CURLOPT_FOLLOWLOCATION, true );
                curl_setopt( $this->curl_session, CURLOPT_HEADER, true );
                curl_setopt( $this->curl_session, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0)' );
    
                $return = curl_exec($this->curl_session);
    
        }
    }
    ?>
    

    【讨论】:

      【解决方案3】:

      您可能需要读取从第一个查询发回的响应标头数据。然后将会话信息(JSSESSION/PHPSESSID/等)作为 cookie 信息传回。

      我认为您的发布数据脚本正在重置 cookie 数据(不确定如何)。当您执行这些操作时,使用 Firebug 监视发送和接收的标头。然后检查 cookie 文件以确保它们没有被覆盖。

      【讨论】:

        【解决方案4】:

        您应该检查 cookie 文件。如果一切正常,它应该有信息。如果没有,也许你应该使用cwd() 来指定一个绝对路径。

        【讨论】:

          【解决方案5】:

          在将 POST 参数指定为数组时,我在使用 curl 时遇到了一些问题。而不是

          curl_setopt( $cu, CURLOPT_POSTFIELDS, $array );
          

          我用:

          curl_setopt( $cu, CURLOPT_POSTFIELDS, http_build_query($array) );
          

          这对我有用。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2015-03-31
            • 2017-10-22
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-03-23
            相关资源
            最近更新 更多