3

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 "\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);

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?

gprime
  • 2,283
  • 7
  • 38
  • 50

3 Answers3

5

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);
netcoder
  • 66,435
  • 19
  • 125
  • 142
3

On ImpressPages I've done it this way:

//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);
}
Mangirdas Skripka
  • 1,647
  • 1
  • 15
  • 14
0

Yes, please loop at the CURLOPT_COOKIEJAR and CURLOPT_COOKIEFILE. I see you already use CURLOPT_COOKIEJAR, so you should probably only dive into *_COOKIEJAR.

Martijn
  • 5,491
  • 4
  • 33
  • 41
  • When I close the curl connection and open it again it does not work even if I add the COOKIEJAR to the loop. – gprime Aug 08 '11 at 20:44
  • Sorry, I ment you should take a look at COOKIEFILE. I'm currently unable to provide you sample code (iPhone). When pointing COOKIEILE to a previously created COOKIEJAR, you'll be logged in at the second cURL exec. – Martijn Aug 08 '11 at 21:55