Jump to content

cURL and Cookies


Fabis94

Recommended Posts

1. I want cURL to use cookies from my browser (i use firefox). So i have the cookies.sqlite file. I'm guessing i can't use that so i used the cookies export extension and now i've got a file like this:

 

.ea.com	TRUE	/	FALSE	1294567511	__utmz	103303007.1278799512.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)
.ea.com	TRUE	/	FALSE	1341871511	__utma	103303007.1717195267.1278799512.1278799512.1278799512.1
.ea.com	TRUE	/	FALSE	1281477904	displayCountrySelector	true

 

Can the cookie file read a file like this? Or does it need a different syntax?

 

2. How can i make cURL read those cookies?

 

P.S. What i want to do is access a site with cURL, but make it read cookies from a file that contains cookies exported from my browser.

Link to comment
Share on other sites

Well okay, but then can i use the file that i posted in my first post? (The one with the .ea.com cookies)

And do i have to do something like this:

 

$c = curl_setopt($ch, CURLOPT_COOKIE, 'cookie.txt');
$cookie = curl_setopt($ch, COOKIE_FILE, $c);

Link to comment
Share on other sites

Just change the path to the file that you will read and write cookies from and to. The file you are on about. Your syntax is incorrect. Simple as.

<?php
$cookiefile = "/path/to/the/file/containing/cookies.txt";
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiefile);   
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile);
?>

Link to comment
Share on other sites

Ok then what syntax would be correct for the cookie file?

Not a clue. Could look like anything, depends on what the website is storing in a cookie. What you have posted looks like cookie data. I was referring to your php syntax, not your cookie file.

 

Why don't you just try it and see if you get the expected results returned via a cURL request?

Link to comment
Share on other sites

Okay well this is what i have:

 

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://my.ign.com/home');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$cookiefile = "cookie.txt";
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiefile);   
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile);
$out = curl_exec($ch);
echo $out;
?>

 

Well it loads the page like if i wasn't logged in (and i am logged in, with my cookies) so what. Maybe i need to make it actually make NEW cookies and log in? Well for that i tried to make this:

 

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://my.ign.com/user/ign-login');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$cookiefile = "cookies.txt";
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiefile);   
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'email=sss&r=http://my.ign.com/home&password=sss');
$out = curl_exec($ch);
echo $out;
?>

 

(i changed the user details of course)

The thing is it just loads the login script and stays at it, while if you log in manually, it redirects back afterwards. How do i make it load the main page after doing this login_script and use the new cookies it made from logging in?

Link to comment
Share on other sites

Oh well now the problem with the redirect is that it redirects the browser to http://my.ign.com (obviously because its the home page), but i need it to LOAD my.ign.com with the new cookies inside my script.

 

So i think that after doing the logging in, i need to use curl again, but to load the main page with the cookies it created while running curl the first time (with CURLOPT_POSTFIELDS as the user details) and all of this needs to be in the same script.

 

So do i just create a new instance of curl_init() or what?

 

If i do it like this, then again it loads the page like if i hadn't logged in (and no cookies were created):

 

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://my.ign.com/user/ign-login');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$cookiefile = "cookies.txt";
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiefile);   
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'email=sss&r=http://my.ign.com/home&password=sss');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
$outz = curl_exec($ch);
///
$chz = curl_init();
curl_setopt($chz, CURLOPT_URL, 'http://my.ign.com/home');
curl_setopt($chz, CURLOPT_POST, 1);
curl_setopt($chz, CURLOPT_RETURNTRANSFER, 1);
$cookiefile = "cookies.txt";
curl_setopt($chz, CURLOPT_COOKIEJAR, $cookiefile);   
curl_setopt($chz, CURLOPT_COOKIEFILE, $cookiefile);
curl_setopt($chz, CURLOPT_FOLLOWLOCATION, TRUE);
$out = curl_exec($chz);
echo $out;
?>

 

(The CURLOPT_POSTFIELDS part isn't the cause, cuz in my script i use the real details)

 

EDIT: Wait never mind it seems it does log in...but well the problem now is that each time i use the script it logs in and then reads the site. I want to get the cookies, save them in a text file and then change the script so it doesnt need to log in each time the script is ran, but  the thing is that the cookies.txt file is empty. If the logged in page does load then it must have loaded the cookies, but they aren't in the file :(

Link to comment
Share on other sites

Correct. You are better creating a cURL function / set of functions i.e

<?php

/*
cURL POST request
*/
function cURLpost($target, $data = false, $cookiePath) {
$ch = curl_init();
$queryString = queryString($data);
if(isset($queryString)) {
	curl_setopt($ch, CURLOPT_POSTFIELDS, $queryString);
}
curl_setopt($ch, CURLOPT_POST, TRUE); 
curl_setopt($ch, CURLOPT_HTTPGET, FALSE); 
curl_setopt($ch, CURLOPT_NOBODY, FALSE);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiePath);   
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiePath);
curl_setopt($ch, CURLOPT_URL, $target);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);     
$result = curl_exec($ch);
curl_close($ch);
return $result;
}

/*
cURL GET request
*/
function cURLget($target, $data = false, $cookiePath) {
$ch = curl_init($target);
$queryString = queryString($data);
if(isset($queryString)) {
	$target = $target."?".$queryString;
}
curl_setopt($ch, CURLOPT_HTTPGET, TRUE); 
curl_setopt($ch, CURLOPT_POST, FALSE); 
curl_setopt($ch, CURLOPT_NOBODY, FALSE);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiePath);   
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiePath);
curl_setopt($ch, CURLOPT_URL, $target);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);     
$result = curl_exec($ch);
curl_close($ch);
return $result;
}

/*
build a query string
*/
function queryString($dataArray = false) {
if(!is_array($dataArray)) {
	return false;	
}
foreach($dataArray as $key => $value) {
	if(strlen(trim($value)) > 0) {
		$value = is_array($value) ? $value : urlencode($value);
		$tempString[] = $key . "=" . $value;
	}
	else {
		$tempString[] = $key;
	}
}
$queryString = join('&', $tempString);
return $queryString;
}



/*
usage
*/
$cookiePath = "/tmp/cookies.txt";
$data['username'] = 'test';
$data['password'] = 'test123';
$data['submit']   = 'login';
// login
$result = cURLpost("http://www.foobar.com/login", $data, $cookiePath);
// go to my account
$result = cURLget("http://www.foobar.com/my-account", false, $cookiePath);
print "<xmp>";
print $result;
print "</xmp>";
?>

Link to comment
Share on other sites

The directory it resides in must have the correct permissions for the apache user to create the file. I am not a windows user. In a Linux setup I would use /tmp as the directory for cookies to be created. The directory gets cleaned when not in use.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.