user1019144
user1019144

Reputation: 1253

php cURL cookie saving empty

I have made a successful connection using cURL, but I trying to download a file and its not working. I know it's the cookie because when I open the downloaded file, it contains an authorized required message. The initial authentication is working though. any thoughts

// Create a curl handle to a non-existing location
$ch = curl_init('https://xxxx/');


// curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);


// ENABLE HTTP POST
curl_setopt ($ch, CURLOPT_POST, 1);

// SET POST PARAMETERS : FORM VALUES FOR EACH FIELD
curl_setopt ($ch, CURLOPT_POSTFIELDS, 'user=xxx&password=xxx');

// IMITATE CLASSIC BROWSER'S BEHAVIOUR : HANDLE COOKIES
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt');



curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);

$fp = fopen("status.xml", "w");

curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);


if(curl_exec($ch) === false)
{
    echo 'Curl error: ' . curl_error($ch);
}
else
{
    echo 'Operation completed without any errors';
}


curl_close($ch);


fclose($fp);

Upvotes: 0

Views: 3949

Answers (2)

TuanNguyen
TuanNguyen

Reputation: 1046

Cookies information just be saved into file after the handle closed, like it said in manual:

CURLOPT_COOKIEJAR => The name of a file to save all internal cookies to when the handle is closed, e.g. after a call to curl_close.

Manual: curl_setopt Besides, you may need to take this into consideration:

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

Upvotes: 1

safarov
safarov

Reputation: 7804

CURLOPT_COOKIEFILE is for reading cookies, CURLOPT_COOKIEJAR is for writing.

curl_setopt ($ch, CURLOPT_COOKIEJAR, '/path/to/cookie.txt');

Make sure you have full path for cookie file and it have permission to be written

Upvotes: 1

Related Questions