Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/226375-save-cookies-with-curl/
Share on other sites

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

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

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

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
)

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?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.