2011-08-08 85 views
3

我正在寫一個腳本從密碼保護成員區下載文件。我現在通過使用curl調用來登錄然後下載。但我想解決的問題是,我可能希望有一個腳本登錄並保存該cookie,然後另一個腳本使用該cookie來下載所需的文件。現在我不確定這是否可能。cUrl登錄然後cUrl下載

這是我工作的代碼:

$cookie_file_path = "downloads/cookie.txt"; 
$fp = fopen($cookie_file_path, "w"); 
fclose($fp); 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_HEADER, false); 
curl_setopt($ch, CURLOPT_NOBODY, false); 
curl_setopt($ch, CURLOPT_URL, $loginUrl); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 

curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path); 

curl_setopt($ch, CURLOPT_USERAGENT, 
    "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $loginPostInfo); 
curl_exec($ch); 

// harddcode some known data 
$downloadSize = 244626770; 

$chuckSize = 1024*2048;   
$filePath = "downloads/file.avi"; 

$file = fopen($filePath, "w"); 

$downloaded = 0; 
$startTime = microtime(true); 

while ($downloaded < $downloadSize) {    
    // DOWNLOAD 
    curl_setopt($ch, CURLOPT_RANGE, $downloaded."-".($downloaded + $chuckSize - 1)); 

    curl_setopt($ch, CURLOPT_URL, $downloadUrl); 
    $result = curl_exec($ch); 

    $nowTime = microtime(true); 
    fwrite($file, $result); 

    echo "\n\nprogress: ".$downloaded."/".$downloadSize." - %".(round($downloaded/$downloadSize, 4) * 100); 

    $downloaded += $chuckSize; 

    // calculate kbps 
    $totalTime = $nowTime - $startTime;    
    $kbps = $downloaded/$totalTime; 

    echo "\ndownloaded: ".$downloaded." bytes"; 
    echo "\ntime: ".round($totalTime, 2); 
    echo "\nkbps: ".(round($kbps/1024, 2)); 
} 

fclose($file); 
curl_close($ch); 

所以是有可能的登錄curl_exec後關閉卷曲,然後再打開一個捲曲的呼叫使用我的登錄部分中保存的cookie來下載文件?

回答

5

是的,這是可能的。

CURLOPT_COOKIEJAR是Cookie的寫入路徑,而CURLOPT_COOKIEFILE是Cookie的讀取路徑。如果您提供CURLOPT_COOKIEFILE與你同CURLOPT_COOKIEJAR做了同樣的路徑,嫋嫋將持續存在跨請求餅乾:

curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path); 
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path); 
+0

謝謝,這工作,並感謝兩者之間的解釋。你搖滾! – gprime

0

是的,請在CURLOPT_COOKIEJAR和CURLOPT_COOKIEFILE循環。我看到你已經使用CURLOPT_COOKIEJAR,所以你應該只能潛入* _COOKIEJAR。

+0

當我關閉卷曲連接並再次打開,即使將COOKIEJAR添加到循環中也不起作用。 – gprime

+0

對不起,我覺得你應該看看COOKIEFILE。我目前無法提供示例代碼(iPhone)。將COOKIEILE指向先前創建的COOKIEJAR時,您將在第二個cURL執行程序中登錄。 – Martijn

3

在ImpressPages我已經做到了這種方式:

//initial request with login data 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, 'http://www.example.com/login.php'); 
curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/32.0.1700.107 Chrome/32.0.1700.107 Safari/537.36'); 
curl_setopt($ch, CURLOPT_POST, true); 
curl_setopt($ch, CURLOPT_POSTFIELDS, "username=XXXXX&password=XXXXX"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_COOKIESESSION, true); 
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie-name'); //could be empty, but cause problems on some hosts 
curl_setopt($ch, CURLOPT_COOKIEFILE, '/var/www/ip4.x/file/tmp'); //could be empty, but cause problems on some hosts 
$answer = curl_exec($ch); 
if (curl_error($ch)) { 
    echo curl_error($ch); 
} 

//another request preserving the session 

curl_setopt($ch, CURLOPT_URL, 'http://www.example.com/profile'); 
curl_setopt($ch, CURLOPT_POST, false); 
curl_setopt($ch, CURLOPT_POSTFIELDS, ""); 
$answer = curl_exec($ch); 
if (curl_error($ch)) { 
    echo curl_error($ch); 
}