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);
}
Related posts:

March 2nd, 2010 9:57 am
Muchas gracias…. good example.
March 4th, 2010 9:27 pm
[...] facebook hay que usar un script (Gracias Alste): <?php // // change Facebook status with curl // Thanks to Alste (curl stuff inspired by [...]
March 22nd, 2010 1:31 am
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.
March 22nd, 2010 4:11 pm
Wow, this seems to be a new behaviour of facebook…
March 25th, 2010 8:02 am
hi, thanks, this script works for me
April 17th, 2010 10:07 am
guys its working fine.
Thank you very much
April 17th, 2010 11:48 pm
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.
April 29th, 2010 2:48 pm
Its not working on my end.. Throws no error but the status is also not Updated at the same time.
May 11th, 2010 10:36 am
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!
May 19th, 2010 10:33 am
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
May 19th, 2010 10:37 am
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)
June 21st, 2010 10:35 am
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.
July 19th, 2010 9:42 am
Hi Guys,
This is not working for me. NO error but status still not updated
July 28th, 2010 3:09 pm
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.
July 28th, 2010 3:10 pm
the “public” comes because I’ve cutted and pasted the code from the MINI BOT CLASS.
July 28th, 2010 3:11 pm
I’ve fixed it. but you have to remove the single qoutes from the setFacebookStatus call.
July 28th, 2010 3:12 pm
Try now, I’ve modified the function (facebook changed its behaviour).
July 29th, 2010 5:57 am
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’);
July 29th, 2010 3:00 pm
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.
July 29th, 2010 3:27 pm
Chris, do you have write permission? The script has to save the my_cookies.txt file with session data to handle the login process.
July 29th, 2010 3:57 pm
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…
July 29th, 2010 5:09 pm
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.
July 29th, 2010 5:18 pm
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
July 29th, 2010 5:22 pm
… 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]);
July 29th, 2010 5:43 pm
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);August 29th, 2010 3:29 pm
[...] 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 [...]
August 30th, 2010 2:34 pm
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.
August 30th, 2010 3:02 pm
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.