Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am writing a script to download files from a password protected members area. I have it working right now by using a curl call to login and then download. But the issue I am trying to fix is that I could like to have a script login and save the cookie then another script use the cookie to download the file needed. Now I am not sure if this is possible.

Here is my working code:

$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 "

progress: ".$downloaded."/".$downloadSize." - %".(round($downloaded / $downloadSize, 4) * 100);

    $downloaded += $chuckSize;

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

    echo "
downloaded: ".$downloaded." bytes";
    echo "
time: ".round($totalTime, 2);
    echo "
kbps: ".(round($kbps / 1024, 2));
}

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

So is it possible to close the curl after the login curl_exec and then open a curl call again to download the file using the cookie I saved during the login part?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
716 views
Welcome To Ask or Share your Answers For Others

1 Answer

Yes it's possible.

CURLOPT_COOKIEJAR is the write path for cookies, while CURLOPT_COOKIEFILE is the read path for cookies. If you provide CURLOPT_COOKIEFILE with the same path as you did with CURLOPT_COOKIEJAR, cURL will persist the cookies across requests:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...