We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,535 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

Facebook Login with cURL

By shubhamjain1 on Jun 18th, 2010 9:09 am

By this script you can remotely login into facebook account with your password and username and fetch/send data.

<?php

/* EDIT EMAIL AND PASSWORD */
$EMAIL      = "";
$PASSWORD   = "";

function cURL($url, $header=NULL, $cookie=NULL, $p=NULL)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HEADER, $header);
    curl_setopt($ch, CURLOPT_NOBODY, $header);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_COOKIE, $cookie);
    curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

    if ($p) {
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $p);
    }
    $result = curl_exec($ch);

    if ($result) {
        return $result;
    } else {
        return curl_error($ch);
    }
    curl_close($ch);
}

$a = cURL("https://login.facebook.com/login.php?login_attempt=1",true,null,"email=$EMAIL&pass=$PASSWORD");
preg_match('%Set-Cookie: ([^;]+);%',$a,$b);
$c = cURL("https://login.facebook.com/login.php?login_attempt=1",true,$b[1],"email=$EMAIL&pass=$PASSWORD");
preg_match_all('%Set-Cookie: ([^;]+);%',$c,$d);
for($i=0;$i<count($d[0]);$i++)
    $cookie.=$d[1][$i].";";

/*
NOW TO JUST OPEN ANOTHER URL EDIT THE FIRST ARGUMENT OF THE FOLLOWING FUNCTION.
TO SEND SOME DATA EDIT THE LAST ARGUMENT.
*/
echo cURL("http://www.facebook.com/",null,$cookie,null);
?>

Can you post a snippet to use the Facebook API using pure cURL without involving any javascript stuff ?
I wish to use the Facebook-Oauth in a project by this method.

nileshgr
Junior Poster
165 posts since Aug 2009
Reputation Points: 17
Solved Threads: 23
Skill Endorsements: 0

Any idea on how I could make good use of this wonderful looking code :)

kyrilkong
Newbie Poster
2 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

It does not wotk, I get blank facebook login page, and not logged in.

Can u post a new solution if possible, please?

Best Reg,

ulas

ozkurede
Newbie Poster
1 post since Sep 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

are you sure this script works? facebook has a number of hidden input fields on the login form; don't all those need to be submitted as well?

jogjayr
Newbie Poster
1 post since Nov 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
<?php
////////////////////////////////////////
//Code By Kerim YILMAZ
//www.ayazoglu.org
////////////////////////////////////////
@$cookie_file_path = "c.txt";
// c.txt die bi dosya yapın cookie ler kayıt için..
$fp = fopen($cookie_file_path, "w");
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";
$kullaniciadin = "faceboook email";
$sifren = "şifren";
$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_file_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=".$kullaniciadin."&pass=".$sifren."");
$html = curl_exec($ch);
$bol = explode('<head>', $html);
$oku = explode('Hesap', $bol[1]);
if($oku){
// Üyelik var işlemle
}else{
// Üyelik Yok işlemler
}
//Giriş yaptıktan sonra anasayfayı yazar
echo $bol[1;]
?>

this code very good ;)

ayazoglu
Newbie Poster
1 post since Mar 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

when i run the above code i got the following
Cookies Required

Cookies are not enabled on your browser. Please adjust this in your security preferences before continuing.

jayapalchandran
Newbie Poster
6 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
itpankaj
Newbie Poster
1 post since Sep 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Yes you can. But there is small problem with Google Crome browser. It doesn't update the page or it doesn't execute the refresh command. You need to use extra code for that browser to get it done...

vijay424
Newbie Poster
1 post since Sep 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

this is not working.. my account fb still disconected even after run this code..
=,="

MrOBY
Newbie Poster
1 post since Jun 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

ayazoglu türk olduğun nerden belli charset= :)))))))

muhammedhunkar
Newbie Poster
1 post since Jan 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.1095 seconds using 2.7MB