Tuesday 3 April 2012

PHP auto login Facebook via Curl.

The following code shows the trick on how to login your facebook via PHP Curl:
  

<?php

// script name: login_to_facebook.php

// coder: Sony AK Knowledge Center - www.sony-ak.com



// your facebook credentials

$username = "----";

$password = "-----";


// access to facebook home page (to get the cookies)

$curl = curl_init ();

curl_setopt ( $curl, CURLOPT_URL, "http://www.facebook.com" );

curl_setopt ( $curl, CURLOPT_FOLLOWLOCATION, 1 );

curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 );

curl_setopt ( $curl, CURLOPT_ENCODING, "" );

curl_setopt ( $curl, CURLOPT_COOKIEJAR, getcwd () . '/cookies_facebook.cookie' );

curl_setopt ( $curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)" );

$curlData = curl_exec ( $curl );

curl_close ( $curl );


// do get some parameters for login to facebook

$charsetTest = substr ( $curlData, strpos ( $curlData, "name=\"charset_test\"" ) );

$charsetTest = substr ( $charsetTest, strpos ( $charsetTest, "value=" ) + 7 );

$charsetTest = substr ( $charsetTest, 0, strpos ( $charsetTest, "\"" ) );


$locale = substr ( $curlData, strpos ( $curlData, "name=\"locale\"" ) );

$locale = substr ( $locale, strpos ( $locale, "value=" ) + 7 );

$locale = substr ( $locale, 0, strpos ( $locale, "\"" ) );


$lsd = substr ( $curlData, strpos ( $curlData, "name=\"locale\"" ) );

$lsd = substr ( $lsd, strpos ( $lsd, "value=" ) + 7 );

$lsd = substr ( $lsd, 0, strpos ( $lsd, "\"" ) );


// do login to facebook

$curl = curl_init ();

curl_setopt ( $curl, CURLOPT_URL, "https://login.facebook.com/login.php?login_attempt=1" );

curl_setopt ( $curl, CURLOPT_FOLLOWLOCATION, 1 );

curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 );

curl_setopt ( $curl, CURLOPT_POST, 1 );

curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, false );

curl_setopt ( $curl, CURLOPT_POSTFIELDS, "charset_test=" . $charsetTest . "&locale=" . $locale . "&non_com_login=&email=" . $username . "&pass=" . $password . "&charset_test=" . $charsetTest . "&lsd=" . $lsd );

curl_setopt ( $curl, CURLOPT_ENCODING, "" );

curl_setopt ( $curl, CURLOPT_COOKIEFILE, getcwd () . '/cookies_facebook.cookie' );

curl_setopt ( $curl, CURLOPT_COOKIEJAR, getcwd () . '/cookies_facebook.cookie' );

curl_setopt ( $curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)" );

//$curlData is the html of your facebook page

$curlData = curl_exec ( $curl );


?>

帮自己宣传一下刚上线的网站 梦奇网;目前的提供的服务是 签证搜索 和 时间和日期。谢谢大家支持,SEO下!

20 comments:

  1. Lib.php is facebook php sdk library??

    ReplyDelete
    Replies
    1. pls refer it again. i have updated.tkx!

      Delete
  2. This comment has been removed by the author.

    ReplyDelete
  3. It will give me HTML curl response how to get user ACCESS token any ideas?

    ReplyDelete
  4. thank you its what i have been looking for!!

    ReplyDelete
  5. hey, THANKS A LOT!
    I have a question, how to access other page right after login?
    this code doesnt give me the results of the request:

    // do login to facebook
    $curl = curl_init ();
    curl_setopt ( $curl, CURLOPT_URL, "https://login.facebook.com/login.php?login_attempt=1" );
    curl_setopt ( $curl, CURLOPT_FOLLOWLOCATION, 1 );
    curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 );
    curl_setopt ( $curl, CURLOPT_POST, 1 );
    curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, false );
    curl_setopt ( $curl, CURLOPT_POSTFIELDS, "charset_test=" . $charsetTest . "&locale=" . $locale . "&non_com_login=&email=" . $username . "&pass=" . $password . "&charset_test=" . $charsetTest . "&lsd=" . $lsd );
    curl_setopt ( $curl, CURLOPT_ENCODING, "" );
    curl_setopt ( $curl, CURLOPT_COOKIEFILE, getcwd () . '/cookies_facebook.cookie' );
    curl_setopt ( $curl, CURLOPT_COOKIEJAR, getcwd () . '/cookies_facebook.cookie' );
    curl_setopt ( $curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)" );
    //$curlData is the html of your facebook page
    $curlData = curl_exec ( $curl );

    // scrape some data yo
    $curl = curl_init ();
    curl_setopt ( $curl, CURLOPT_URL, "http://www.facebook.com/ajax/shares/view/?target_fbid=410558838979218&__a=1" );
    curl_setopt ( $curl, CURLOPT_FOLLOWLOCATION, 1 );
    curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 );
    curl_setopt ( $curl, CURLOPT_ENCODING, "" );
    curl_setopt ( $curl, CURLOPT_COOKIEFILE, getcwd () . '/cookies_facebook.cookie' );
    curl_setopt ( $curl, CURLOPT_COOKIEJAR, getcwd () . '/cookies_facebook.cookie' );
    curl_setopt ( $curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)" );

    $curlData = curl_exec ( $curl );
    echo $curlData;


    Thanks again

    ReplyDelete
  6. Never mind. Perhaps, there is no need to init the curl again. After login, you use the initialized curl to get the data.

    // scrape some data yo
    curl_setopt ( $curl, CURLOPT_URL, "http://www.facebook.com/ajax/shares/view/?target_fbid=410558838979218&__a=1" );

    $curlData = curl_exec ( $curl );

    Pls have a try. Thanks:)

    ReplyDelete
  7. I have question about password. Since my password contains special character "%" and FB returned incorrect password(I am sure my password is correct). How do I solve this ??? It need encoding ???

    ReplyDelete
  8. Brilliant! This code works great, thank you for sharing! Would it be possible to authorize a Facebook app via curl, too?

    ReplyDelete
  9. I have a error:

    Warning: curl_setopt() [function.curl-setopt]: CURLOPT_FOLLOWLOCATION cannot be activated when safe_mode is enabled or an open_basedir is set in /home/sandersi/public_html/fb/logar.php on line 13

    ?????????????? I NEED THIS SCRIPT....
    Please, My Email is: sanderluis@hotmail.com

    sorry, I speak portuguese; hahaha
    tks;

    ReplyDelete
  10. Its giving error Cookies are not enabled on your browser. Please adjust this in your security preferences before continuing.
    Please let me know what to do

    ReplyDelete
  11. hi Nice Work Dude,It helped a lot :)

    Could you please help me out by providing similar example for TripAdvisor

    Thanks and Regards

    ReplyDelete
  12. Pretty interesting post! Thanks it was interesting. on website

    ReplyDelete
  13. after executing curl i want user id can i do that with this code
    please help me as soon as possible. my email is dhamecharahul@gmail.com.

    i have added below code after your code php sdk for that but each time it will return 0 instead of user id

    // Create our Application instance (replace this with your appId and secret).
    $facebook = new Facebook(array(
    'appId' => 'app id',
    'secret' => 'app key',
    ));

    // Get User ID
    echo $userId = $facebook->getUser();

    ReplyDelete
  14. perfect,it's very usefull,thank you very much.

    ReplyDelete
  15. can i call multiple usernames and passwords from db by using this cUrl functionality

    ReplyDelete
  16. Hi Allen,
    I have no words to say thanks because by this post i have resolved an issue that make me stuck for a week . Hats off for you.
    Really thanks

    ReplyDelete