Fabis94 Posted August 2, 2010 Share Posted August 2, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/209579-curl-and-cookies/ Share on other sites More sharing options...
JonnoTheDev Posted August 2, 2010 Share Posted August 2, 2010 curl_setopt($ch, CURLOPT_COOKIEFILE, '/path/to/cookie.file'); Look at the options available http://uk3.php.net/manual/en/function.curl-setopt.php Quote Link to comment https://forums.phpfreaks.com/topic/209579-curl-and-cookies/#findComment-1094098 Share on other sites More sharing options...
Fabis94 Posted August 2, 2010 Author Share Posted August 2, 2010 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); Quote Link to comment https://forums.phpfreaks.com/topic/209579-curl-and-cookies/#findComment-1094102 Share on other sites More sharing options...
JonnoTheDev Posted August 2, 2010 Share Posted August 2, 2010 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/209579-curl-and-cookies/#findComment-1094104 Share on other sites More sharing options...
Fabis94 Posted August 2, 2010 Author Share Posted August 2, 2010 Ok then what syntax would be correct for the cookie file? Quote Link to comment https://forums.phpfreaks.com/topic/209579-curl-and-cookies/#findComment-1094107 Share on other sites More sharing options...
JonnoTheDev Posted August 2, 2010 Share Posted August 2, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/209579-curl-and-cookies/#findComment-1094112 Share on other sites More sharing options...
Fabis94 Posted August 2, 2010 Author Share Posted August 2, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/209579-curl-and-cookies/#findComment-1094123 Share on other sites More sharing options...
JonnoTheDev Posted August 2, 2010 Share Posted August 2, 2010 You need to follow redirects <?php curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); ?> Quote Link to comment https://forums.phpfreaks.com/topic/209579-curl-and-cookies/#findComment-1094125 Share on other sites More sharing options...
Fabis94 Posted August 2, 2010 Author Share Posted August 2, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/209579-curl-and-cookies/#findComment-1094128 Share on other sites More sharing options...
JonnoTheDev Posted August 2, 2010 Share Posted August 2, 2010 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>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/209579-curl-and-cookies/#findComment-1094133 Share on other sites More sharing options...
Fabis94 Posted August 2, 2010 Author Share Posted August 2, 2010 Well ok thanks that works. I still don't understand why it didn't even create a tmp/cookies.txt file :/ Quote Link to comment https://forums.phpfreaks.com/topic/209579-curl-and-cookies/#findComment-1094139 Share on other sites More sharing options...
JonnoTheDev Posted August 2, 2010 Share Posted August 2, 2010 Permissions to create a file in the directory specified? Quote Link to comment https://forums.phpfreaks.com/topic/209579-curl-and-cookies/#findComment-1094141 Share on other sites More sharing options...
Fabis94 Posted August 2, 2010 Author Share Posted August 2, 2010 I'm testing this on my windows computer (with xampp) by the way. And well the cookies.txt file isn't read only. Quote Link to comment https://forums.phpfreaks.com/topic/209579-curl-and-cookies/#findComment-1094143 Share on other sites More sharing options...
JonnoTheDev Posted August 2, 2010 Share Posted August 2, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/209579-curl-and-cookies/#findComment-1094147 Share on other sites More sharing options...
Fabis94 Posted August 2, 2010 Author Share Posted August 2, 2010 But if it doesn't create it, then how does it log in and use the cookies? :/ Quote Link to comment https://forums.phpfreaks.com/topic/209579-curl-and-cookies/#findComment-1094155 Share on other sites More sharing options...
JonnoTheDev Posted August 2, 2010 Share Posted August 2, 2010 How do you know the site sets any cookies after login? Setup a test php script on your local server that sets a cookie. Use the cURL functions I gave you to make a request to it. Is a cookie set? Quote Link to comment https://forums.phpfreaks.com/topic/209579-curl-and-cookies/#findComment-1094156 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.