Geekalicio.us

24 April 2009

Logging into Facebook with PHP and CURL

logging-into-facebook-with-php-and-curl

Here’s a simple little PHP function using CURL that I use to login to facebook to automate some of my ‘daily chores’.  It logs the account in, and stores the session in the cookie, so you can do other functions.

Short and simple source code…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
 
/*
* Login to facebook
* $login_email : Account to login with
* $login_pass : Account password
*
* Returns true if logged in successfully, false otherwise
* Echoes any login error code
*
* Matt Smith - geekalicio.us
* Apr 23, 2009
*/
function fb_login($login_email, $login_pass){
 
 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, 'https://login.facebook.com/login.php?login_attempt=1');
 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&locale=en_US&email='.urlencode($login_email).'&pass='.urlencode($login_pass).'&pass_placeholder=&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');
 curl_setopt($ch, CURLOPT_POST, 1);
 curl_setopt($ch, CURLOPT_HEADER, 0);
 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
 curl_setopt($ch, CURLOPT_COOKIEJAR, str_replace('\\','/',dirname(__FILE__)).'/fb_cookies.txt');
 curl_setopt($ch, CURLOPT_COOKIEFILE, str_replace('\\','/',dirname(__FILE__)).'/fb_cookies.txt');
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.6) Gecko/2009011913 Firefox/3.0.6 (.NET CLR 3.5.30729)");
 curl_exec($ch);
 
 $err = 0;
 $err = curl_errno($ch);
 curl_close($ch);
 
 if ($err != 0){
 echo 'error='.$err."\n";
 return(false);
 } else {
 return(true);
 }
 
}
  • Share/Save/Bookmark
Tags: curl, facebook, php
logging-into-facebook-with-php-and-curl
  • ebley
    Dude - you rock. This is a great script - worked first time. I used it and modified it to get the list of fans for a page - since that is not an item in the facebook api. If anyone wants it it is at: http://www.elbsolutions.com/public_scripts/
  • Hello, can you zip the file??
    I want to make an auto post homepage wall.. ex. posting a link
  • irfankhokhar
    This is Login script in curl
    function signRequest($args)
    {
    ksort($args);
    $sig = '';
    foreach($args as $k => $v)
    {
    $sig[] .= $k . '=' . $v;
    }

    $sig = implode ('&', $sig);

    return $sig;
    }

    //$login_email = 'email'
    //$login_pass = 'pass'

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://login.facebook.com/login.php');
    curl_setopt($ch, CURLOPT_POSTFIELDS,'email='.urlencode($login_email).'&pass='.urlencode($login_pass));
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_COOKIEJAR, str_replace('\\','/',dirname(__FILE__)).'/fb_cookies.txt');
    curl_setopt($ch, CURLOPT_COOKIEFILE, str_replace('\\','/',dirname(__FILE__)).'/fb_cookies.txt');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.6) Gecko/2009011913 Firefox/3.0.6 (.NET CLR 3.5.30729)");
    curl_exec($ch);

    curl_setopt($ch, CURLOPT_URL, 'https://login.facebook.com/code_gen.php?api_key=0e984031112826b0b6e4130662c74588&v=1.0');
    $data = curl_exec($ch);

    preg_match_all('/<input.*?value\\s*=\\s*"?([^\\s>"]*)".*?name\\s*=\\s*"?([^\\s>"]*)"/i', $data, $hiddens,PREG_PATTERN_ORDER) ;

    $args = array ('email' => $login_email,
    'pass' => $login_pass,
    'fb_dtsg' => 'QCsWr',
    'generate' => '1',
    'v' => "1.0",
    'api_key' => '$api kye',
    'post_form_id' => $hiddens[1][0],
    'submit' => 'go'
    );


    $arg = signRequest($args);

    curl_setopt($ch, CURLOPT_URL, 'https://login.facebook.com/code_gen.php');
    curl_setopt($ch, CURLOPT_POSTFIELDS,$arg);
    curl_exec($ch);

    $data = curl_exec($ch);


    curl_close($ch);

    print_R($data);
  • vishal1980
    Hello,

    I have tried this but not understand the behaviour. On my first request it allow me to login but next request it shows

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

    I want to publish data on my wall. I have tried to login and then I have requested my page using same curl object but sometimes it shows this cookie error.
  • Can i buy this domain from you?
  • You can buy any domain from me - It's all a question of price :) - Feel free to contact me offline - quaffapint -at- gmail.com .
  • david_214
    with this code, i can post message to my wall. I wonder there is a way to post message to the business page?

    Thanks
  • zied
    awesome, but is it possible to do the same operation without using php, only dealing with the pure curl and not curl libraries of php ?
  • Thanks!
    The trick was in defining the cookie file properly.
  • moonam
    Hi Matt. Thank you for sharing the interesting code.
    Somehow, I can't login the facebook using your code. If I visit the facebook.com after I run your code on my web page. I think I should be logined in the facebook, but it isn't. Can you provide more details? Thank you for your help.
  • moonam
    The facebook said,
    "Cookies Required
    Cookies are not enabled on your browser. Please adjust this in your security preferences before continuing."
  • Hey moonam,

    Sorry that's not how it works. Its simply code to allow you to automate things via curl/php. It does not interact with your web browser, or place any authentication cookie that your browser will use. It's just if you wanted to script a little facebook automation :-).
  • This code doesn't work man. There is something wrong with the cookie.

    So after you got logged in. If you click on any link, you'll get logged out right away.
  • Are you logged into facebook elsewhere? I noticed that it kicks me out of the gui while my scripts are running.

    I use it to run some 'automated processes'. During the loops, I'll keep checking the login state, and if its logged out, it just re-logs in again.

    matt
  • Hasn't Robert Scoble been suspended for these kind of hacks? IIRC, he had a programm who would export his contact-data. But I think FB did not like to be scripted.
blog comments powered by Disqus