Mar 01 2010

PHP curl bot to update Facebook status

Category: Php,Spiders & webbotsGiulio Pons @ 10:30 pm

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);
}
  • Share/Bookmark

Related posts:

  1. PHP to post on a Facebook page
  2. Test if a remote url exists with PHP and CURL
  3. Posting to Facebook from website with Facebook Connect
  4. Tiny url encode and decode with PHP
  5. PHP Web page to text function

Tags: , , , , ,

28 Responses to “PHP curl bot to update Facebook status”

  1. eduardo says:

    Muchas gracias…. good example.

  2. DiarioLinux » Actualizar Twitter y Facebook con curl says:

    [...] facebook hay que usar un script (Gracias Alste): <?php // // change Facebook status with curl // Thanks to Alste (curl stuff inspired by [...]

  3. James says:

    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.

  4. admin says:

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

  5. tamill says:

    hi, thanks, this script works for me

  6. balut says:

    guys its working fine.
    Thank you very much

  7. david says:

    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.

  8. Abbas Dhuliawala says:

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

  9. kit says:

    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!

  10. fb says:

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

  11. fb says:

    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)

  12. Kit says:

    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.

  13. sumon says:

    Hi Guys,

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

  14. admin says:

    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.

  15. admin says:

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

  16. admin says:

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

  17. admin says:

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

  18. QuijoteShin says:

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

  19. Chris says:

    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.

  20. admin says:

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

  21. Chris says:

    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…

  22. admin says:

    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.

  23. Chris says:

    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

  24. Chris says:

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

  25. admin says:

    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);
    
  26. Barattalo » PHP to post on a Facebook page says:

    [...] I’ve modified the Mini Bot Class, I’ve fixed the Facebook status update and I’ve implemented the function to post on the wall of a facebook page: suppose you have a [...]

  27. Chris says:

    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.

  28. Giulio Pons says:

    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.

Leave a Reply