Page 1 of 1

cURL code - question

Posted: Wed Jul 28, 2010 5:16 pm
by WishfulThinker
Hello,
I've written a code using cURL. The code is working but I can't understand something.
The code (below) logs into my Facebook account, then return the main member-page.
As you can see, it consists of 2 URL calls, one to the login page (with POST data) and the second to the main member-page.

I found that on the first URL call, as you can see, I must insert this condition to get it work:

if( ! $result = curl_exec($ch))
{
trigger_error(curl_error($ch));
}
curl_close($ch);

If I don't insert that snippet - the code is not working, and just returns a blank page.

Could someone please explain me why that condition is necessary to get it work?
Thanks

Here is my Code (can also be found at http://pastie.org/1064646):

Code: Select all

<?php

$cookie_file_path = "/home/vhosts/gooliver.eu5.org/cookie.txt";
$url = 'https://login.facebook.com/login.php';
$reffer = ' https://login.facebook.com/login.php?login_attempt=1';
$agent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.6) Gecko/20100625 Firefox/3.6.6 (.NET CLR 3.5.30729)";

$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&return_session=0&legacy_return=1&display=&session_key_only=0&trynum=1&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&lsd=jpm0D&email=********&pass=********&login=%D7%9B%D7%A0%D7%99%D7%A1%D7%94';

$header_array[0]= "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
$header_array[1]= "Accept-Language: en-us,en;q=0.5";
$header_array[2]= "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
$header_array[3]= "Keep-Alive: 115";
$header_array[4]= "Connection: keep-alive";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header_array);
curl_setopt($ch, CURLOPT_REFERER, $reffer);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $POSTFIELDS);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
$result = curl_exec($ch);

  if( ! $result = curl_exec($ch))
    {
        trigger_error(curl_error($ch));
    }
    curl_close($ch);


curl_close ($ch);

$url = 'http://www.facebook.com/home.php';
$agent = "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.6) Gecko/20100625 Firefox/3.6.6 (.NET CLR 3.5.30729)";
$header_array[0]= "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
$header_array[1]= "Accept-Language: en-us,en;q=0.5";
$header_array[2]= "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
$header_array[3]= "Keep-Alive: 115";
$header_array[4]= "Connection: keep-alive";


$c = curl_init();
curl_setopt($c, CURLOPT_URL,$url);
curl_setopt($c, CURLOPT_USERAGENT, $agent);
curl_setopt($c, CURLOPT_HTTPHEADER, $header_array);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($c, CURLOPT_COOKIEFILE, $cookie_file_path);
$page = curl_exec($c);


curl_close($c);
echo $page;
?>


clarification

Posted: Wed Jul 28, 2010 6:18 pm
by WishfulThinker
I just found out that if I repeat twice the curl_exec - it works (even without the condition). So must be
$result = curl_exec($ch);
$result = curl_exec($ch);

I don't understand why it needs executing twice to log me in properly into Facebook?

Updated code here:

Code: Select all

<?php

$cookie_file_path = "/home/vhosts/gooliver.eu5.org/cookie.txt";
$url = 'https://login.facebook.com/login.php';
$reffer = ' https://login.facebook.com/login.php?login_attempt=1';
$agent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.6) Gecko/20100625 Firefox/3.6.6 (.NET CLR 3.5.30729)";

$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&return_session=0&legacy_return=1&display=&session_key_only=0&trynum=1&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&lsd=jpm0D&email=********&pass=********&login=%D7%9B%D7%A0%D7%99%D7%A1%D7%94';

$header_array[0]= "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
$header_array[1]= "Accept-Language: en-us,en;q=0.5";
$header_array[2]= "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
$header_array[3]= "Keep-Alive: 115";
$header_array[4]= "Connection: keep-alive";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header_array);
curl_setopt($ch, CURLOPT_REFERER, $reffer);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $POSTFIELDS);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);

$result = curl_exec($ch);
$result = curl_exec($ch);

curl_close ($ch);

$url = 'http://www.facebook.com/home.php';
$agent = "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.6) Gecko/20100625 Firefox/3.6.6 (.NET CLR 3.5.30729)";
$header_array[0]= "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
$header_array[1]= "Accept-Language: en-us,en;q=0.5";
$header_array[2]= "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
$header_array[3]= "Keep-Alive: 115";
$header_array[4]= "Connection: keep-alive";


$c = curl_init();
curl_setopt($c, CURLOPT_URL,$url);
curl_setopt($c, CURLOPT_USERAGENT, $agent);
curl_setopt($c, CURLOPT_HTTPHEADER, $header_array);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($c, CURLOPT_COOKIEFILE, $cookie_file_path);
$page = curl_exec($c);


curl_close($c);
echo $page;
?>