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&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'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);
?>
AMAZING!
ReplyDeletethank 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