Login into Facebook (and others servers) with cURL

2.6k Views Asked by At

The code looks ok, if you insert incorrect login infos appears "incorrect", but if you insert the correct infos, the cURL returns the login page, rather than the homepage (with friends and mural).

Why this happens, what is missing?

Code:

$login_email = 'YOUR MAIL';
$login_pass = 'YOUR PASS';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.facebook.com/login.php');
curl_setopt($ch, CURLOPT_POSTFIELDS,'email='.urlencode($login_email).'&pass='.urlencode($login_pass).'&login=Login');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIE, "fr=0DlGgNm7j7OSrxwX6.AWW7LPYiS5UCE2Hd72fGrJQiQLs.BVmtC5.sI.FX7.0.AWX65Dnb; lu=SAj6vCvJVQ5w62Kbyx2DPvEw; datr=n9CaVRge8gdmQM4fbYPCgerZ; locale=pt_BR; reg_fb_ref=https%3A%2F%2Fwww.facebook.com%2F%3Fstype%3Dlo%26jlou%3DAfe0qy59dIr6vAHnkp81bUe_fBoQZvzbSCC-DlC6PTUNZappnx7W6Mx8L1ujMMa_jNbUpui0M7_YUfiLiuGWWzrgnEQc9k1no4kFe50usSdB9A%26smuh%3D24564%26lh%3DAc9EK2ltZxJOphMY; reg_fb_gate=https%3A%2F%2Fwww.facebook.com%2F%3Fstype%3Dlo%26jlou%3DAfe0qy59dIr6vAHnkp81bUe_fBoQZvzbSCC-DlC6PTUNZappnx7W6Mx8L1ujMMa_jNbUpui0M7_YUfiLiuGWWzrgnEQc9k1no4kFe50usSdB9A%26smuh%3D24564%26lh%3DAc9EK2ltZxJOphMY; wd=1708x436; dpr=0.800000011920929");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3");
curl_setopt($ch, CURLOPT_REFERER, "http://www.facebook.com");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$page = curl_exec($ch) or die(curl_error($ch));

echo $page;

Thanks, in advance.

2

There are 2 best solutions below

0
Samuel Kelemen On

Important is to understand login process. I see you send a lot of cookies with your curl request but i think this cookies are not alive already.

You need to do two curl requests:

  1. Make curl request on fb login page with right credentials
  2. You get response with lot of set-cookie headers
  3. Save all data from this headers and set this data to second curl request on facebook page
  4. You requests with this cookies data will be handled as from logged user
0
Mohamed Fawsan On
$login_email = 'your email';
$login_pass = 'your password';

//Simple cURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://m.facebook.com/login.php');
curl_setopt($ch, CURLOPT_POSTFIELDS, 'charset_test=€,´,€,´,水,Д,Є&email=' . urlencode($login_email) . '&pass=' . urlencode($login_pass) . '&login=Login');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept-Charset: utf-8',
    'Accept-Language: en-us,en;q=0.7,bn-bd;q=0.3',
    'Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5'));
curl_setopt($ch, CURLOPT_COOKIEFILE, getcwd() . '/mirazmac_cookie.txt'); // The cookie file
curl_setopt($ch, CURLOPT_COOKIEJAR, getcwd() . '/mirazmac_cookie.txt'); // cookie jar
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windo`enter code here`ws; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3");
curl_setopt($ch, CURLOPT_REFERER, "http://m.facebook.com");
$fbMain = curl_exec($ch) or die(curl_error($ch));
//Blocking Direct Access to file
if (eregi("fb.php", $_SERVER['PHP_SELF'])) {
    die("<p><h2>Access Denied!</h2><h4>You don't have right permission to access this file directly.<br/>Contact MiraZ Mac for more information.</h4></p>");
}