2013-10-22_102424

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);
}
  1. How to change twitter status with php and curl without oAuth
  2. PHP to post on a Facebook page
  3. Test if a remote url exists with PHP and CURL
  4. How to read facebook likes count from PHP
  5. Send push notification to iPhone with PHP and pushme.to

57 comments

  1. Saad

    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?

  2. winrol

    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

  3. Pingback: Favoriti | Daniele Milana

Post a comment

You may use the following HTML:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>