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

Marzo 1, 2010

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);
}

Author

PHP expert. Wordpress plugin and theme developer. Father, Maker, Arduino and ESP8266 enthusiast.

Comments on “PHP curl bot to update Facebook status”

57 thoughts

  1. eduardo ha detto:

    Muchas gracias…. good example.

  2. James ha detto:

    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.

  3. admin ha detto:

    Wow, this seems to be a new behaviour of facebook…

  4. tamill ha detto:

    hi, thanks, this script works for me

  5. balut ha detto:

    guys its working fine.
    Thank you very much

  6. david ha detto:

    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.

  7. Abbas Dhuliawala ha detto:

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

  8. kit ha detto:

    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!

  9. fb ha detto:

    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 ;-)

  10. fb ha detto:

    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)

  11. Kit ha detto:

    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.

  12. sumon ha detto:

    Hi Guys,

    This is not working for me. NO error but status still not updated

  13. admin ha detto:

    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.

  14. admin ha detto:

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

  15. admin ha detto:

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

  16. admin ha detto:

    Try now, I’ve modified the function (facebook changed its behaviour).

  17. QuijoteShin ha detto:

    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’);

  18. Chris ha detto:

    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.

  19. admin ha detto:

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

  20. Chris ha detto:

    Hi admin,

    Yes i do, i see the cookie-file and it contains data. the last step returns the following error:
    6 – Could not resolve host: m.facebook.comhttp:; Host not found

    Also the dump of curl_getinfo looks funny (the url part)
    [url] => http://m.facebook.comhttp://upload.facebook.com/mobile_upload.php?r194a387d&refid=7&r0e7b13c1

    Looks like something with building the target url goes wrong…

  21. admin ha detto:

    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.

  22. Chris ha detto:

    admin, i am using 1:1 the code you postet above.

    i took a closer look at the forms of the code contained in the $page variable.
    there are multiple forms in the page, the action for the status-form looks like this:
    /a/home.php?r365c9390&refid=7&raa791dcf

    but there is another form (where i suspect the above mentioned url comes from) where the action reads like this:
    http://upload.facebook.com/mobile_upload.php?rb42eeec1&refid=7&raaa5e3d9

  23. Chris ha detto:

    … 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]);

  24. admin ha detto:

    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); 	
    
  25. Chris ha detto:

    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.

  26. Giulio Pons ha detto:

    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.

  27. Eric Wargo ha detto:

    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

  28. Ignacio ha detto:

    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;
    }

  29. pam ha detto:

    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?

  30. rokis ha detto:

    how could we update fan pages status with this coding?

  31. ae oreang ha detto:

    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

  32. Giulio Pons ha detto:

    Please, add a link to my site from your post.

  33. Giulio Pons ha detto:

    Nmh… You should understand which variable with [1] is the error.

  34. Giulio Pons ha detto:

    You can use the code in the mini bot class as written here: https://www.barattalo.it/2010/07/28/php-to-post-on-facebook-page/

  35. Giulio Pons ha detto:

    I think it’s possible, but I don’t have that code yet! :-)

  36. ae oreang ha detto:

    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

  37. ae oreang ha detto:

    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

  38. Joe Tech ha detto:

    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?

  39. Giulio Pons ha detto:

    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.

  40. Giulio Pons ha detto:

    Mmm when I told you to add the link, probably, I’ve answered to the wrong comment. Sorry.

  41. Taofiq Ben ha detto:

    Good Job
    Can anyone give me a working Code Please

  42. TI ha detto:

    This is not working for me. NO error but status still not updated.

    I changed the https://www.barattalo.it/mini-bots-php-class/ 591 lines of:
    preg_match (‘/ action ='(.*?)’/’, $ form_ar [1] [3], $ form_num);
    I do not fetch the URL correctly but it did not help.

    Please someone help.

  43. curl ha detto:

    The example on http://360percents.com/posts/php-curl-status-update-working-example-sep-2010/ still works… There is also an example of how to update Facebook Page.

    Best regards

  44. chopkowy ha detto:

    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 :)

  45. roberto ha detto:

    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!

  46. Melissa ha detto:

    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)

  47. automobile.it ha detto:

    Very good tip! Thanks!

  48. Saad ha detto:

    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?

  49. Rylee Lore ha detto:

    A round of applause for your blog post.Really thank you! Much obliged.

  50. winrol ha detto:

    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

  51. Heya i am for the first time here. I came across this board and I in finding It really helpful & it helped me out much. I hope to provide something again and help others like you aided me.

  52. hai ha detto:

    I can’t not find post_form_id on page after login.
    Can anyone help? Thanks so much!

Comments are closed

Recommended

PHP to post on a Facebook page

Hi, I’ve modified the Mini Bot Class, I’ve fixed the Facebook status update and I’ve implemented the function to post…

Luglio 28, 2010

How to read facebook likes count from PHP

When you add facebook like button to your site, probably, you also want to save the number of likes of…

Ottobre 8, 2012

PHP bot to get wikipedia definitions

Wikipedia, the collaborative and multilingual encyclopedia project, has a lot of usefull terms defined in its database, you can find…

Agosto 29, 2010

Social buttons: the fastest way for WordPress, without plugins

NOTE: the code in this post is written for WordPress but you can easily translate it in any language. You’re here…

Settembre 15, 2015

VK Social metrics, Top Stories plugin now with support for Vkontakte

VK SOCIAL METRICS VK.COM, also known as Vkontakte, is an important russian social network, which has over 270 million users.…

Novembre 19, 2014

Make a cron job with IFTTT

Cron is a software utility, a time-based job scheduler in Unix-like computer operating systems. People who set up and maintain…

Novembre 12, 2013