ankhmor Posted February 1, 2011 Share Posted February 1, 2011 I have the following script and I've been trying to make it work for hours $tmpfname = "temp\cookie.txt"; $curl_handle=curl_init(); $user_agent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; Trident/5.0)"; curl_setopt($curl_handle,CURLOPT_URL,'url goes here'); curl_setopt($curl_handle, CURLOPT_USERAGENT, $user_agent); curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,FALSE); curl_setopt ($curl_handle, CURLOPT_HEADER, TRUE); curl_setopt ($curl_handle, CURLOPT_COOKIEJAR, $tmphandle); curl_setopt ($curl_handle, CURLOPT_COOKIEFILE, $tmphandle); curl_exec($curl_handle); curl_close($curl_handle); } There are 2 problems. 1. the most important is that the cookies don't save. However in the header that i get there's plenty of Set-Cookie: 2. I've set CURLOPT_RETURNTRANSFER to false, however I still see the webpage when I run cURL. I even deleted the portion where I echoed the buffer (I used to have CURLOPT_RETURNTRANSFER set to true). I used to have this portion of the code if (empty($buffer)) { print "Sorry, unable to retrive site.<p>"; } else { print $buffer; } but all the buffer printed out was "1" (without the quotes) Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/226375-save-cookies-with-curl/ Share on other sites More sharing options...
ManiacDan Posted February 1, 2011 Share Posted February 1, 2011 Look into Snoopy(), it's a PHP class that masquerades as a browser, including proper cookie handling. -Dan Quote Link to comment https://forums.phpfreaks.com/topic/226375-save-cookies-with-curl/#findComment-1168438 Share on other sites More sharing options...
AbraCadaver Posted February 1, 2011 Share Posted February 1, 2011 I haven't used cURL in a while, but the most blatant error is that you set $tmpfname but then use $tmphandle in the setopt. Quote Link to comment https://forums.phpfreaks.com/topic/226375-save-cookies-with-curl/#findComment-1168441 Share on other sites More sharing options...
ChemicalBliss Posted February 1, 2011 Share Posted February 1, 2011 Make sure the temp folder exists. Also, this should be enough to get cookies working: $tmpfname = "temp\cookie.txt"; $curl_handle=curl_init(); $user_agent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; Trident/5.0)"; curl_setopt($curl_handle,CURLOPT_URL,'url goes here'); curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1); curl_setopt ($curl_handle, CURLOPT_COOKIEJAR, $tmpfname); curl_setopt ($curl_handle, CURLOPT_COOKIEFILE, $tmpfname); $output = curl_exec($curl_handle); $info = curl_close($curl_handle); echo "<!--// Look in page-source (html) to view this properly: \n", print_r($info), "\n //-->\n", $output; As AbraCadaver stated, you should fix your consistency errors (variables names should stay the same for ex): curl_setopt ($curl_handle, CURLOPT_COOKIEJAR, $tmphandle); curl_setopt ($curl_handle, CURLOPT_COOKIEFILE, $tmphandle); Should be: curl_setopt ($curl_handle, CURLOPT_COOKIEJAR, $tmpfname); curl_setopt ($curl_handle, CURLOPT_COOKIEFILE, $tmpfname); And use integer boolean values (0,1) instead of string boolean (true,false) when defining cURL Options. For example: curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1); hope this helps Quote Link to comment https://forums.phpfreaks.com/topic/226375-save-cookies-with-curl/#findComment-1168448 Share on other sites More sharing options...
ankhmor Posted February 1, 2011 Author Share Posted February 1, 2011 As AbraCadaver stated, you should fix your consistency errors (variables names should stay the same for ex): curl_setopt ($curl_handle, CURLOPT_COOKIEJAR, $tmphandle); curl_setopt ($curl_handle, CURLOPT_COOKIEFILE, $tmphandle); Should be: curl_setopt ($curl_handle, CURLOPT_COOKIEJAR, $tmpfname); curl_setopt ($curl_handle, CURLOPT_COOKIEFILE, $tmpfname); And use integer boolean values (0,1) instead of string boolean (true,false) when defining cURL Options. For example: curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1); hope this helps That didn't do the trick actually. Also the part where echo "<!--// Look in page-source (html) to view this properly: \n", print_r($info), "\n //-->\n", $output; in page source $info shows up as 1 and it is before the rest of the page however if i set CURLOPT_RETURNTRANSFER to 0 then i end up with the following on the bottom of page source <!--// Look in page-source (html) to view this properly: 1 //--> 1 Quote Link to comment https://forums.phpfreaks.com/topic/226375-save-cookies-with-curl/#findComment-1168460 Share on other sites More sharing options...
ManiacDan Posted February 1, 2011 Share Posted February 1, 2011 print_r($something,true) instead of print_r($something). The second argument, when true, returns the item as a string so it can be used in an echo like this. Otherwise, the output is weird. -Dan Quote Link to comment https://forums.phpfreaks.com/topic/226375-save-cookies-with-curl/#findComment-1168516 Share on other sites More sharing options...
ChemicalBliss Posted February 1, 2011 Share Posted February 1, 2011 My bad, these last few lines should be: $output = curl_exec($curl_handle); $info = curl_getinfo($curl_handle); curl_close($curl_handle); echo "<!--// Look in page-source (html) to view this properly: \n", print_r($info, true), "\n //-->\n", $output; hope this helps Quote Link to comment https://forums.phpfreaks.com/topic/226375-save-cookies-with-curl/#findComment-1168537 Share on other sites More sharing options...
ankhmor Posted February 2, 2011 Author Share Posted February 2, 2011 I get this Array ( [url] => the page url [content_type] => text/html [http_code] => 200 [header_size] => 476 [request_size] => 153 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 0.063 [namelookup_time] => 0 [connect_time] => 0.016 [pretransfer_time] => 0.016 [size_upload] => 0 [size_download] => 8447 [speed_download] => 134079 [speed_upload] => 0 [download_content_length] => 8447 [upload_content_length] => -1 [starttransfer_time] => 0.063 [redirect_time] => 0 ) Quote Link to comment https://forums.phpfreaks.com/topic/226375-save-cookies-with-curl/#findComment-1168634 Share on other sites More sharing options...
ankhmor Posted February 2, 2011 Author Share Posted February 2, 2011 I tried using $cookie_file_path = "cookie.txt"; $fp = fopen($cookie_file_path,'wb'); fclose($fp); $agent = "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7"; $reffer = "http://www.facebook.com/login.php"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,"https://login.facebook.com/login.php"); curl_setopt($ch, CURLOPT_USERAGENT, $agent); curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_fie_path); curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path); curl_setopt($ch, CURLOPT_REFERER, "http://www.facebook.com/login.php"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($ch, CURLOPT_POSTFIELDS, "charset_test=%E2%82%AC%2C%C2%B4%2C%E2%82%AC%2C%C2%B4%2C%E6%B0%B4%2C%D0%94%2C%D0%84&version=1.0&return_session=0&charset_test=%E2%82%AC%2C%C2%B4%2C%E2%82%AC%2C%C2%B4%2C%E6%B0%B4%2C%D0%94%2C%D0%84&email=user%40gmail.com&pass=0000000"); $html = curl_exec($ch); echo $html; but the cookie file is empty and facebook tells me that i don't have cookies enabled. wtf? Quote Link to comment https://forums.phpfreaks.com/topic/226375-save-cookies-with-curl/#findComment-1168697 Share on other sites More sharing options...
ankhmor Posted February 2, 2011 Author Share Posted February 2, 2011 figured it out. for some reason the cookie file was being created in the root directory of xampp and not in the htdocs/path Quote Link to comment https://forums.phpfreaks.com/topic/226375-save-cookies-with-curl/#findComment-1168723 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.