PHP curl bot to update Facebook status

I’ve found this great mini bot from Alste blog, and I’ve decided to add it to the mini bot class. This bot uses curl to connect to facebook mobile (m.facebook.com) and perform the login. Then it saves the cookies received from mobile facebook and go to the facebook mobile homepage where it sets the status making a post.
I’ve tried to make the same thing with the normal facebook, but it didn’t work. I think that mobile facebook is simpler and easier to make bots working. I’ve added this bot to the Mini Bot PHP class.

NEW
Today (28/07/2010) I’ve modified the function to handle more variables from facebook forms and it seems to work again.

//
// change Facebook status with curl
// Thanks to Alste (curl stuff inspired by nexdot.net/blog)
function setFacebookStatus($status, $login_email, $login_pass, $debug=false) {
	//CURL stuff
	//This executes the login procedure
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, 'https://login.facebook.com/login.php?m&next=http%3A%2F%2Fm.facebook.com%2Fhome.php');
	curl_setopt($ch, CURLOPT_POSTFIELDS, 'email=' . urlencode($login_email) . '&pass=' . urlencode($login_pass) . '&login=' . urlencode("Log in"));
	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, "my_cookies.txt");
	curl_setopt($ch, CURLOPT_COOKIEFILE, "my_cookies.txt");
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	//make sure you put a popular web browser here (signature for your web browser can be retrieved with 'echo $_SERVER['HTTP_USER_AGENT'];'
	curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.12) Gecko/2009070611 Firefox/3.0.12");
	curl_exec($ch);

	//This executes the status update
	curl_setopt($ch, CURLOPT_POST, 0);
	curl_setopt($ch, CURLOPT_URL, 'http://m.facebook.com/home.php');
	$page = curl_exec($ch);

	//echo htmlspecialchars($page);

	curl_setopt($ch, CURLOPT_POST, 1);
	//this gets the post_form_id value
	preg_match("/input type=\"hidden\" name=\"post_form_id\" value=\"(.*?)\"/", $page, $form_id);
	preg_match("/input type=\"hidden\" name=\"fb_dtsg\" value=\"(.*?)\"/", $page, $fb_dtsg);
	preg_match("/input type=\"hidden\" name=\"charset_test\" value=\"(.*?)\"/", $page, $charset_test);
	preg_match("/input type=\"submit\" class=\"button\" name=\"update\" value=\"(.*?)\"/", $page, $update);

	//we'll also need the exact name of the form processor page
	//preg_match("/form action=\"(.*?)\"/", $page, $form_num);
	//sometimes doesn't work so we search the correct form action to use
	//since there could be more than one form in the page.
	preg_match_all("#<form([^>]*)>(.*)</form>#Ui", $page, $form_ar);
	for($i=0;$i<count($form_ar[0]);$i++)
		if(stristr($form_ar[0][$i],"post_form_id")) preg_match("/form action=\"(.*?)\"/", $page, $form_num);

	$strpost = 'post_form_id=' . $form_id[1] . '&status=' . urlencode($status) . '&update=' . urlencode($update[1]) . '&charset_test=' . urlencode($charset_test[1]) . '&fb_dtsg=' . urlencode($fb_dtsg[1]);
	if($debug) {
		echo "Parameters sent: ".$strpost."<hr>";
	}
	curl_setopt($ch, CURLOPT_POSTFIELDS, $strpost );

	//set url to form processor page
	curl_setopt($ch, CURLOPT_URL, 'http://m.facebook.com' . $form_num[1]);
	curl_exec($ch);

	if ($debug) {
		//show information regarding the request
		print_r(curl_getinfo($ch));
		echo curl_errno($ch) . '-' . curl_error($ch);
		echo "<br><br>Your Facebook status seems to have been updated.";
	}
	//close the connection
	curl_close($ch);
}

Anything to say?

Comments with Facebook or below with WordPress



WordPress comments:

  1. Dangerous. does not work. says a connection to fb mobile is being made from a computer, it locks down your facebook temporarily “for your own security” and when you sign back into your facebook you have to go through a set of questions to unlock your account.. be careful.

  2. It works, however if you use it on more than one account it will lock out the second account. You must recover the account by verifying information.

  3. Its not working on my end.. Throws no error but the status is also not Updated at the same time.

  4. Hi!

    Thanks for this example. I keep on getting on the following : “public function setFacebookStatus” not too sure why though. If anyone can help? Cheers!

  5. First add this at the beginning:
    at the end.

    And delete public from line (number 04):
    function setFacebookStatus($status, $login_email, $login_pass) {

    That worked for me ;-)

  6. Hmm something wrong with the formatting on my last post ;-)

    But just delete “public” from line 04 and it will work

    (Hopefully… It did for me)

  7. Hey Guys,

    Thanks for the code. Afraid it is not working for me. Could anyone advise? I have deleted the ‘public’ at the start as stated by ‘fb’. My code calling the function looks like:

    $status = “hello world”;
    $login_email = “myemail@email.com”;
    $login_pass = “mypassword”;
    $update = setFacebookStatus(‘$status’, ‘$login_email’, ‘$login_pass’);

    Is this correct?

    Many Thanks.

  8. I’ve fixed the function inside the Mini Bot Class. Now it searches also some hidden fields and send also them, now (this means today 28/07/2010) it works.

  9. the “public” comes because I’ve cutted and pasted the code from the MINI BOT CLASS.

  10. I’ve fixed it. but you have to remove the single qoutes from the setFacebookStatus call.

  11. KIT:

    your function must be

    Hey Guys,

    Thanks for the code. Afraid it is not working for me. Could anyone advise? I have deleted the ‘public’ at the start as stated by ‘fb’. My code calling the function looks like:

    $update = setFacebookStatus($status, $login_email, $login_pass);

    NOT

    $update = setFacebookStatus(’$status’, ‘$login_email’, ‘$login_pass’);

  12. Hi,

    First off all, thanx for this script.
    But it seems like despite of the “Your Facebook status seems to have been updated.” message, the facebook status is not updated :(

    I tried the class-version and the “standalone” version of the script.

  13. Chris, do you have write permission? The script has to save the my_cookies.txt file with session data to handle the login process.

  14. Chris, you are passing a wrong page address (you’re using the code to post to a page wall) I think. The link of the target page should not have the host part of the address, but only the page. Please write the line of the call to the function.

  15. … fixed it. a little dirty, but it works.

    instead of

    curl_setopt($ch, CURLOPT_URL, ‘http://m.facebook.com’ . $form_num[1]);

    now i do

    $tmp = explode(“http://upload.facebook.com/mobile_upload.php?”,$form_num[1]);
    curl_setopt($ch, CURLOPT_URL, ‘http://m.facebook.com/a/home.php?’ . $tmp[1]);

  16. Chris! Very strange. I don’t have that form, maybe it’s different from italy. I’ve another one in the page but not that one for mobile uploads, just one for a search function. Your fix it’s dirty, yes! :-) I should change the function to look for the correct form to send in a different way.
    This fix is better (I think), I’ve added to the code above. It searches the form that has the post_form_id found before:

    	preg_match_all("#<form([^>]*)>(.*)</form>#Ui", $page, $form_ar);
    	for($i=0;$i<count($form_ar[0]);$i++) if(stristr($form_ar[0][$i],"post_form_id")) preg_match("/form action=\"(.*?)\"/", $page, $form_num); 	
    

  17. Does not work. The $form_num will consist of:
    [0] => form action=”https://m.facebook.com/findfriends.php?view=email&r79baa153&refid=7″
    [1] => https://m.facebook.com/findfriends.php?view=email&r79baa153&refid=7

    Request is made to URL:
    http://m.facebook.comhttps://m.facebook.com/findfriends.php?view=email&r79baa153&refid=7

    And error is:
    6 – Couldn’t resolve host ‘m.facebook.comhttps:’

    Obviously something is amiss in that script. The same script used as above, with the exception of added error debuging.

  18. Mmm I’ve just runned the script and it still works. So, I think that the problem is different. The link found is the one to “find friends”… so the page you are targetting probably is different from the normal page I receive after the login process. I don’t know, but now, from Italy, it works.

  19. Worked for me. All I did was copy the code above and then underneath it all I added:

    $status = “hello world”;
    $login_email = “eric@example.com”;
    $login_pass = “mypassword”;
    $update = setFacebookStatus($status, $login_email, $login_pass);

    This will update the FB account. Now all that needs to be done is to turn this into a form so I can write out what I want it to say.

    Great script!

    -e

  20. Mi dirty fix for the findfriends url problem:

    function setFacebookStatus($status, $debug=false) {

    $login_email = ‘XXX’;
    $login_pass = ‘XXX’;

    //CURL stuff
    //This executes the login procedure
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, ‘https://login.facebook.com/login.php?m&next=http%3A%2F%2Fm.facebook.com%2Fhome.php’);
    curl_setopt($ch, CURLOPT_POSTFIELDS, ’email=’ . urlencode($login_email) . ‘&pass=’ . urlencode($login_pass) . ‘&login=’ . urlencode(“Log in”));
    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, “my_cookies.txt”);
    curl_setopt($ch, CURLOPT_COOKIEFILE, “my_cookies.txt”);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    //make sure you put a popular web browser here (signature for your web browser can be retrieved with ‘echo $_SERVER[‘HTTP_USER_AGENT’];’
    curl_setopt($ch, CURLOPT_USERAGENT, “Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.12) Gecko/2009070611 Firefox/3.0.12”);
    curl_exec($ch);

    //This executes the status update
    curl_setopt($ch, CURLOPT_POST, 0);
    curl_setopt($ch, CURLOPT_URL, ‘http://m.facebook.com/home.php’);
    $page = curl_exec($ch);

    //echo htmlspecialchars($page);

    curl_setopt($ch, CURLOPT_POST, 1);
    //this gets the post_form_id value
    preg_match(“/input type=\”hidden\” name=\”post_form_id\” value=\”(.*?)\”/”, $page, $form_id);
    preg_match(“/input type=\”hidden\” name=\”fb_dtsg\” value=\”(.*?)\”/”, $page, $fb_dtsg);
    preg_match(“/input type=\”hidden\” name=\”charset_test\” value=\”(.*?)\”/”, $page, $charset_test);
    preg_match(“/input type=\”submit\” class=\”button\” name=\”update\” value=\”(.*?)\”/”, $page, $update);

    //we’ll also need the exact name of the form processor page
    //preg_match(“/form action=\”(.*?)\”/”, $page, $form_num);
    //sometimes doesn’t work so we search the correct form action to use
    //since there could be more than one form in the page.
    preg_match_all(“#]*)>(.*)#Ui”, $page, $form_ar);
    //for($i=0;$i<count($form_ar[0]);$i++)
    //if(stristr($form_ar[0][$i],"status")) {
    //$page2 = stristr($form_ar[0][$i],"invite");
    //echo "page2: $page2″;
    preg_match(“/form action=\”\/a\/home\.php(.*?)\”/”, $page, $form_num);

    //}
    //$page2 = $form_ar[0][3];
    //echo $page2;
    //preg_match(“/form action=\”(.*?)\”/”, $page2, $form_num);

    $strpost = ‘post_form_id=’ . $form_id[1] . ‘&status=’ . urlencode($status) . ‘&update=’ . urlencode($update[1]) . ‘&charset_test=’ . urlencode($charset_test[1]) . ‘&fb_dtsg=’ . urlencode($fb_dtsg[1]);
    if($debug) {
    echo “Parameters sent: “.$strpost.””;
    }
    curl_setopt($ch, CURLOPT_POSTFIELDS, $strpost );

    if ($debug){
    echo “”;
    echo “a: “;
    print_r($form_ar[0]);
    echo “b: “;
    print_r($form_num);
    echo “”;
    }

    //set url to form processor page

    //curl_setopt($ch, CURLOPT_URL, ‘http://m.facebook.com’ . $form_num[1]);
    curl_setopt($ch, CURLOPT_URL, “http://m.facebook.com/a/home.php”.$form_num[1]);
    if ($debug) echo $extra . “”;
    curl_exec($ch);

    if ($debug) {
    //show information regarding the request
    print_r(curl_getinfo($ch));
    echo curl_errno($ch) . ‘-‘ . curl_error($ch);
    echo “Your Facebook status seems to have been updated.”;
    }
    //close the connection
    curl_close($ch);
    return true;
    }

  21. Thanks for the script it works ok, I would like to know if it’s posible to find a way to getStatus from facebook in the same way?

  22. hi,
    dear all,

    Thank’s for sharing this code, but I still got an error

    A PHP Error was encountered
    Severity: Notice
    Message: Undefined offset: 1
    Filename: plugins/minibots.class.php
    Line Number: 584

    this is line 584 :
    $strpost = ‘post_form_id=’ . $form_id[1] . ‘&status=’ . urlencode($status) . ‘&update=’ . urlencode($update[1]) . ‘&charset_test=’ . urlencode($charset_test[1]) . ‘&fb_dtsg=’ . urlencode($fb_dtsg[1]);

    I use php 5.3, any ide to solve this error..???

    thank’s
    best regard

  23. Thank’s Giulio Pons,

    I have been debug it, I add code on line 568,

    echo curl_errno($ch) . ‘-‘ . curl_error($ch);

    $page = curl_exec($ch);

    echo (htmlspecialchars($page));

    die;

    and I got an error :

    0-Protocol https not supported or disabled in libcurl

    do you know, how to enable https in libcur

    Sory Pons, I don’t know what you mean about “Please, add a link to my site from your post.”

    thank’s

  24. Dear all,

    it was SOLVED, with install manual curl with option –with-ssl

    ceking curl
    $ curl-config –feature
    SSL
    IPv6
    libz
    NTLM

    but, I still got an error,

    A PHP Error was encountered
    Severity: Notice
    Message: Undefined offset: 1
    Filename: plugins/minibots.class.php
    Line Number: 599

    it’s because variable $update doesn’t has value

    but status on facebook updated..

    to remove that error message, just set variable $update=”

    thank’s

  25. This isn’t working for me. It doesn’t give any error, but it doesn’t update my status on FaceBook, either.

    I read a comment on another blog that says that this method is against FB’s terms and conditions and that FB will just automatically log you out if you connect this way. How do we get around this?

  26. I think (but I’m not suer) that facebook log you out if you use different ips for the same account in a small period, but this script was made to update facebook status from a php program… and so should not be used together with your account opened in the browser. Yes I think this method is not completely allowed by Facebook… but this is just a tutorial.

  27. Hi everyone,

    I have been struggling the problem of an Undefined offset : 1..
    I have gone through the source code of the m.facebook.com page and find out that the search patter in preg_match function for the ‘update’ button should be changed

    FROM
    preg_match(“/input type=\”submit\” class=\”button\” name=\”update\” value=\”(.*?)\”/”, $page, $update); // line 33
    TO
    preg_match(“/input type=\”submit\” value=\”(.*?)\” class=\”btn btnC\” name=\”update\”/”, $page, $update);

    Another thing, as I came across two offset issues, is the post_form_id case around line 41, it also should be changed

    FROM
    if(stristr($form_ar[0][$i],”post_form_id”)) preg_match(“/form action=\”(.*?)\”/”, $page, $form_num);
    TO
    if(stristr($form_ar[0][$i],”composer_form”))preg_match(“/action=\”(.*?)\”/”, $page, $form_num);

    At least it worked for me and I hope it will help others as well.

    Kind regards :)

  28. hi… i use facebook from italy and this code don’t work :( tell me this error:

    Notice: Undefined offset: 1 in C:\Programmi\EasyPHP-5.3.3.1\www\face\Senzatitolo-3.php on line 43

    Notice: Undefined offset: 1 in C:\Programmi\EasyPHP-5.3.3.1\www\face\Senzatitolo-3.php on line 50

    the problem is here:

    $form_id[1]

    why????????????

    please help me!

  29. This is not working or me. When i open minibots.class.php i get tons of errors.
    Please help, am i missing something?
    (its uploaded to my server and all that…not an idiot)

  30. Notice: Undefined offset: 1 in C:\xampp\htdocs\fyp\index.php on line 137

    Parameters sent: post_form_id=input type=”hidden” name=”post_form_id” value=”c0fe5f36f4e22b18f6df6531a2cfb9f7″&status=Test&update=&charset_test=input+type%3D%22hidden%22+name%3D%22charset_test%22+value%3D%22%E2%82%AC%2C%C2%B4%2C%E2%82%AC%2C%C2%B4%2C%E6%B0%B4%2C%D0%94%2C%D0%84%22&fb_dtsg=input+type%3D%22hidden%22+name%3D%22fb_dtsg%22+value%3D%22AQD9xNjh%22

    Notice: Undefined offset: 1 in C:\xampp\htdocs\fyp\index.php on line 144
    Array ( [url] => http://m.facebook.com [content_type] => text/html; charset=utf-8 [http_code] => 200 [header_size] => 313 [request_size] => 967 [filetime] => -1 [ssl_verify_result] => 20 [redirect_count] => 0 [total_time] => 8.658 [namelookup_time] => 0 [connect_time] => 0 [pretransfer_time] => 0 [size_upload] => 340 [size_download] => 23918 [speed_download] => 2762 [speed_upload] => 39 [download_content_length] => 0 [upload_content_length] => 340 [starttransfer_time] => 7.987 [redirect_time] => 0 [certinfo] => Array ( ) [redirect_url] => ) 0-

    Your Facebook status seems to have been updated.

    this gives me error. what should i do?

  31. This code works great. My question is how can I modify it to post to my business wall. I set up a test app and am trying to post to it. if i log in to m.facebook.com then go to my business page and click in the address bar I get m.facbook.com/profile.php?id=358287174193361. This is where I want to post. Help would be greatly appreciated. I’ve been looking on the net for days with no luck.

    Thanks in advance.

    winrol

  32. […] List your Behance Feed in WordPress without a Plugin Autenticazione su Facebook con curl e PHP PHP curl bot to update Facebook status – Barattalo Facebook Open Graph Protocol Full Integration Walkthrough | Website Design & Development […]



Sorry, WordPress comments are closed on old posts.