我一直在開發一個能夠登錄到AWeber.com並執行海量數據導入的程序。該腳本使用PHP cURL庫以及它們的CookieJar設置來通過瀏覽器來欺騙普通用戶。當通過cURL發佈網站表單時,「Session expired」和「請啓用cookie」警告
該腳本完美工作,允許登錄和更改列表,但是當涉及發佈表單數據時(在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函數內失敗,第一次還是第二次? – 2010-03-08 10:56:01