Saturday, December 25, 2010

How to post facebook status via backend in PHP

As promised, here's the script on how to submit facebook status using PHP. just follow the simple steps below and you can have a facebook status update on your own website.

1. Again, from my previous post, you should know the structure of the wapsite you wish to access via back-end and for this post, we will access facebook wapsite http://m.facebook.com. check out also my previous post - How to submit facebook status via back-end using PERL

2. Since we are using PHP, we will use CURL to access the site via back-end. make sure you have installed CURL in your server.

3. To start with, input your facebook username/email, password, and status.

$fuser = '<your facebook username/email>';
$fpass = '<your facebook password>';
$status = '<your facebook status>';


4. Initialize CURL as coded below. set the CURLOPT_URL to the login page of the wapsite http://m.facebook.com/login.php to login to the site. set the CURLOPT_POSTFIELDS email and password. then the CURLOPT_COOKIEJAR and CURLOPT_COOKIEFILE.

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://m.facebook.com/login.php');
curl_setopt($ch, CURLOPT_POSTFIELDS,'email='.urlencode($fuser).'&pass='.urlencode($fpass).'&login=Login');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, "/tmp/my_cookies.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "/tmp/my_cookies.txt");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3");
curl_setopt($ch, CURLOPT_REFERER, "http://m.facebook.com");
$page = curl_exec($ch); 


5. CURL_EXEC will output the redirected page which you will need to get PARAMETERS in sending STATUS. you can try printing the variable $page so you will know if the return page is successful or not. I pasted a code snippet of the return page below.

<form method="post" id="composer_form" action="/a/home.php?fbb=rbacd82de&amp;refid=7"><input type="hidden" name="fb_dtsg" value="DdtLy" autocomplete="off" /><input type="hidden" name="post_form_id" value="f224790ed71d0fe6565de44f1b4bbe98" /><input type="hidden" name="charset_test" value="€,´,€,´,水,Д,Є" /><div>What&#039;s on your mind?<br /><textarea class="composerInput" name="status" rows="2" onfocus="" onblur=""></textarea></div><input type="submit" value="Share" class="btn btnC" name="update" /></form>


6. You will need parameters such as FORM ACTION, FB_DTSG, and POST_FORM_ID just like in my previous post. to get these parameters, you should use PREG_MATCH.

//this gets the post_form_id value
preg_match("/input type=\"hidden\" name=\"post_form_id\" value=\"(.*?)\"/", $page, $form_id);
//we'll also need the exact name of the form processor page
preg_match("/form method=\"post\" id=\"composer_form\" action=\"(.*?)\"/", $page, $form_action);
//we'll also need the value of the fb_dtsg
preg_match("/name=\"fb_dtsg\" value=\"(.*?)\"/", $page, $form_fbdtsg);


7. All parameters will be stored as ARRAY in variables $form_id, $form_action, and $form_fbdtsg. you will use this on the code below to send facebook status.

curl_setopt($ch, CURLOPT_URL, "http://m.facebook.com".$form_action[1]);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'post_form_id='.$form_id[1].'&status='.urlencode($status).'&fb_dtsg='.$form_fbdtsg[1].'&update=Share');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, "/tmp/my_cookies.txt");
$page = curl_exec($ch);


8. Upon executing the CURL, you should expect that your STATUS will be posted in your facebook wall. to get the output and information on your CURL execution, you can print CURL_GETINFO. You can also check for errors by printing CURL_ERRNO and CURL_ERROR.

print_r(curl_getinfo($ch));
echo curl_errno($ch) . '-' . curl_error($ch);


9. To summarize everything, you should have a code like this below. happy coding everyone!!

<?

$fuser = '<your facebook username/email>';
$fpass = '<your facebook password>';
$status = '<your facebook status>';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://m.facebook.com/login.php');
curl_setopt($ch, CURLOPT_POSTFIELDS,'email='.urlencode($fuser).'&pass='.urlencode($fpass).'&login=Login');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, "/tmp/my_cookies.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "/tmp/my_cookies.txt");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3");
curl_setopt($ch, CURLOPT_REFERER, "http://m.facebook.com");
$page = curl_exec($ch);

preg_match("/input type=\"hidden\" name=\"post_form_id\" value=\"(.*?)\"/", $page, $form_id);
preg_match("/form method=\"post\" id=\"composer_form\" action=\"(.*?)\"/", $page, $form_action);
preg_match("/name=\"fb_dtsg\" value=\"(.*?)\"/", $page, $form_fbdtsg);

curl_setopt($ch, CURLOPT_URL, "http://m.facebook.com".$form_action[1]);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'post_form_id='.$form_id[1].'&status='.urlencode($status).'&fb_dtsg='.$form_fbdtsg[1].'&update=Share');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, "/tmp/my_cookies.txt");
$page = curl_exec($ch);

print_r(curl_getinfo($ch));
echo curl_errno($ch) . '-' . curl_error($ch);

?>

2 comments:

  1. thank you for the compliment and for reading my post. you might wanna check out this post as well.. using facebook oauth - http://paulgonzaga.blogspot.com/2011/01/posting-facebook-status-using-oauth-via.html

    ReplyDelete

Leadership 101


  • Leadership demands sacrifices for the near-term to receive lasting benefits. the longer we wait to make sacrifices, the harder they become. Successful people make important decisions early in their life, then manage those decisions the rest of their lives.
  • Growth does not happen by chance. If you want to be sure to grow, you need a plan something strategic, specific, and scheduled. it's a discipline that would need incredible determination from us.
  • Success comes by going the extra mile, working the extra hours, and investing the extra time. The same is true for us. If we want to get to excel in any segment of life, a little extra effort can help. Our efforts can go a long way if we only work a little smarter, listen a little better, push a little harder, and persevere a little longer.
  • Making a difference in your work is not about productivity; it's about people. When you focus on others and connect with them, you can work together to accomplish great things.
  • Envision a goal you'd like to reach. Make it big enough to scare you a little. Now write down a plan for moving toward it. Create mini-goals within the big goal, to set yourself up for continual progress. And include some risks, too. Set yourself up for success.
  • Leaders build margins, not image. A leader may be forced to take unpopular stands for the good of the company. Popularity isn't bad, but decisions made solely on the basis of popular opinion can be devastating. So take courage and make the right though sometimes painful choices.