Updating Facebook Status from PHP
Here is the simple example about how to update Facebook status from PHP. We use different approach, just do web scraping to login to Facebook mobile and change the status. It’s simple and fun. Today (July 25, 2010) I update the code again because the previous code is not working anymore due the changes of Facebook mobile interface.
I also put the code on GitHub here http://github.com/sonyarianto/PostStatus2FacebookPHP. Please grab and play with it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | <?php // web scraper: Sony AK Knowledge Center // last update on July 25, 2010 from Bangalore, India // facebook credentials (change to appropriate value) $username = "facebook_username"; $password = "facebook_password"; // do login to facebook $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, "https://login.facebook.com/login.php?m&next=http://m.facebook.com/home.php"); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_POSTFIELDS, "email=" . $username . "&pass=" . $password . "&login=Log In"); curl_setopt($curl, CURLOPT_ENCODING, ""); curl_setopt($curl, CURLOPT_COOKIEJAR, getcwd() . '/cookies_facebook.cookie'); $curlData = curl_exec($curl); curl_close($curl); // do get post url $urlPost = substr($curlData, strpos($curlData, "action=\"/a/home") + 8); $urlPost = substr($urlPost, 0, strpos($urlPost, "\"")); $urlPost = "http://m.facebook.com" . $urlPost; // do get some parameters for updating the status $fbDtsg = substr($curlData, strpos($curlData, "name=\"fb_dtsg\"")); $fbDtsg = substr($fbDtsg, strpos($fbDtsg, "value=") + 7); $fbDtsg = substr($fbDtsg, 0, strpos($fbDtsg, "\"")); $postFormId = substr($curlData, strpos($curlData, "name=\"post_form_id\"")); $postFormId = substr($postFormId, strpos($postFormId, "value=") + 7); $postFormId = substr($postFormId, 0, strpos($postFormId, "\"")); // do update facebook status $statusMessage = "Hey, I can update my facebook status from PHP (July 25, 2010)"; $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $urlPost); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, "fb_dtsg=" . $fbDtsg . "&post_form_id=" . $postFormId . "&status=" . $statusMessage . "&update=Update Status"); curl_setopt($curl, CURLOPT_ENCODING, ""); curl_setopt($curl, CURLOPT_COOKIEFILE, getcwd() . '/cookies_facebook.cookie'); curl_setopt($curl, CURLOPT_COOKIEJAR, getcwd() . '/cookies_facebook.cookie'); $curlData = curl_exec($curl); curl_close($curl); echo "Your Facebook status already updated with '" . $statusMessage . "'"; |
If you have any difficulty, please drop us e-mail to info@sony-ak.com or send comment below. You can read other interesting articles at Sony AK Knowledge Center at www.sony-ak.com.





woww…..keren bgt nehh..thanks bgt ya bro’ ^_*
oia salam kenal yah.
Good tutorial, pak sony.Use curl to transfer and retrieve data from m.facebook.com.
keep for share info n ilmu… jgn hiraukan ahli telematika porno ya….
ada error : “Fatal error: Call to undefined function curl_init() in C:\wamp\www\FB\index.php on line 9″
kenapa neh..?
@chebe itu tandanya php libcurl belum ter-install pada mesin/server Anda, coba check lagi setting di WAMP nya… baca juga article saya seputar checking curl existance di mesin anda.
nice info..
Thanks boss, it useful very much for me. ^_^
wow .. wajib di coba .. .. kayaknya seru neh ..
Wah dpt gratisan code nech, sy bikin ginian tempo hari gak selesai, mhn ijin nyedot…mantap Mas Sony
wuieeew … keren neh
wuieeew .. keren neh
ga ngerti mas..
seeeeeep…..mangtabs
asik nih, mau coba ach
@artha silakan dicoba mas…. ditunggu hasilnya…
Dude your work is great..
Hi,
Your script works great. I have a requirement to sent a friend request automatically. So I expanded your script. After the login steps I followed three steps as in facebook.
1) Getting the friend page which I wish to add as friend (This will bring the public profile with Add as Friend button)
2) Using some values from above response I’m trying to sent a post request (Ones we post when we click the Add as friend button. It will bring a popup)
My code works fine until this.
3) I have another post to add friend. This is the one will get sent when we click the confirm button within the popup. I checked my request and normal browser request both are identical. But I’m getting following error.
for (;;);{“error”:1346001,”errorSummary”:”Validation Error”,”errorDescription”:”",”errorIsWarning”:false,”silentError”:1,”payload”:{“secure”:false,”title”:”Add XXX as a friend?”}}
I’m sure I’m getting the correct response in the second step. Any ideas what should I do to overcome this issue? If you want I’ll email you the full code. Please help me solve this
I think I have posted my comment and a question in the wrong place. My last comment have to be for this post.
http://www.sony-ak.com/2010/03/simulate-login-to-facebook-with-php-and-curl/
Dude, please move that to the right post when you moderate. Waiting to hear a reply soon …
Hey, good post.
Hi,Useful post, although i’m more interested in what will take place out of the google vs facebook social network war. I haven’t read much news on it lately, which most likely implies that it’s not that far off, but i’ve a sense its really going to lead to a few large improvements to social networks. For me, i’d personally prefer facebook came out of the battle still in the lead simply because google’s already large enough, plus they currently hold excessive data on most people. I do not reckon that both have the opportunity to exist though, either google’s social network will control or totally fail. What does everybody else feel?
Hello,
Such a Nice Article. Helped me a lot. I solved all of my Problems. Thanks for posting such a nice and helpful Article.
Thanks & Regards
Rahul Anand