Facebook Login with cURL

shubhamjain1 2 Tallied Votes 8K Views Share

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

MooGeek commented: SICK! +5
<?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);
?>

Dani AI

Generated

If you are trying to POST a username/password to facebook.com with cURL, expect it to fail and risk account/security issues. Facebook’s login form uses dynamic anti-automation fields, cookies, device checks, and 2FA; those hidden inputs change often (as noted above) and scraping violates the platform terms. The supported way to “fetch/send data” is Facebook Login via OAuth 2.0, where the browser handles the user’s login and your server uses cURL only for the token exchange and Graph API calls. See the official guides in Facebook Login and the manual flow at Facebook Login advanced manual flow. OAuth details are standardized in RFC 6749.

Workflow:

  • Redirect the user to https://www.facebook.com/v19.0/dialog/oauth?...client_id,redirect_uri,state,scope....
  • On your redirect URI, verify state, then exchange code for an access token with cURL.
  • Call the Graph API with that token; exchange for a long-lived token if needed.

Example PHP (server-side steps only):

$code = $_GET['code'] ?? null;
$redirectUri = 'https://example.com/fb-callback';
$appId = 'YOUR_APP_ID';
$appSecret = 'YOUR_APP_SECRET';

$tokenUrl = 'https://graph.facebook.com/v19.0/oauth/access_token'
  . '?client_id='.$appId
  . '&redirect_uri='.urlencode($redirectUri)
  . '&client_secret='.$appSecret
  . '&code='.$code;

$ch = curl_init($tokenUrl);
curl_setopt_array($ch, [CURLOPT_RETURNTRANSFER=>true, CURLOPT_SSL_VERIFYPEER=>true]);
$token = json_decode(curl_exec($ch), true)['access_token'] ?? null;

$ch = curl_init('https://graph.facebook.com/v19.0/me?fields=id,name&access_token='.$token);
curl_setopt_array($ch, [CURLOPT_RETURNTRANSFER=>true, CURLOPT_SSL_VERIFYPEER=>true]);
$user = json_decode(curl_exec($ch), true);

Tips: match redirect_uri exactly, never disable SSL verification, keep your app in Live mode with approved permissions, and avoid scraping which violates Meta Platform Terms.

Member Avatar for Member #585571
Member #585571

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.

kyrilkong 0 Newbie Poster

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

ozkurede 0 Newbie Poster

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

Member Avatar for Member #857433
Member #857433

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?

ayazoglu 0 Newbie Poster
<?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,"");
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 ;)

jayapalchandran 0 Newbie Poster

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.

itpankaj 0 Newbie Poster

One good article I found for Facebook login -

vijay424 0 Newbie Poster

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

MrOBY 0 Newbie Poster

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

muhammedhunkar 0 Newbie Poster

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

pozitive 0 Newbie Poster

hahaha, olsun yazmış adam işte :)
bu arada yukardaki kod 10 numara çalışıyor. son cookie yi de session da tutup sitede gezebilirsiniz.

thanks for first code.

Tùng 0 Newbie Poster

Huy Tùng

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.