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 );
?>
Lib.php is facebook php sdk library??
ReplyDeletepls refer it again. i have updated.tkx!
DeleteThis comment has been removed by the author.
ReplyDeleteIt will give me HTML curl response how to get user ACCESS token any ideas?
ReplyDeletethank you its what i have been looking for!!
ReplyDeleteNever mind :)
Deletehey, THANKS A LOT!
ReplyDeleteI 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
Never mind. Perhaps, there is no need to init the curl again. After login, you use the initialized curl to get the data.
ReplyDelete// 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:)
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 ???
ReplyDeleteBrilliant! This code works great, thank you for sharing! Would it be possible to authorize a Facebook app via curl, too?
ReplyDeleteI have a error:
ReplyDeleteWarning: 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;
Its giving error Cookies are not enabled on your browser. Please adjust this in your security preferences before continuing.
ReplyDeletePlease let me know what to do
hi Nice Work Dude,It helped a lot :)
ReplyDeleteCould you please help me out by providing similar example for TripAdvisor
Thanks and Regards
This is truly a great read for me. Dallas Website Design
ReplyDeletePretty interesting post! Thanks it was interesting. on website
ReplyDeleteafter executing curl i want user id can i do that with this code
ReplyDeleteplease 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();
perfect,it's very usefull,thank you very much.
ReplyDeletecan i call multiple usernames and passwords from db by using this cUrl functionality
ReplyDeletePLease tell me how to re login using cookies
ReplyDeleteHi Allen,
ReplyDeleteI 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