Thursday, May 27, 2010

Update Facebook Status with PHP - Most Simple Script!

5/27/2010

It would be so nice for a web developer if she could allow the users to let them update their Facebbok status from her website only. This tutorial helps you develop the similar script. It shopew how to update the facebook status using PHP from your website. Just checkout the live demo...
Tutorial borrowed from dave-earley.com





Live Demo Download Script

PHP Code

<body>
<?php
if (empty($_POST['username']) || empty($_POST['password']) || empty($_POST['status']))
{
 echo 'Please fill in all fields.';
 //die();
}

if (isset($_POST['username']))
{
 $status   = $_POST['status'];
 $username = $_POST['username'];
 $password = $_POST['password'];
 //Used for debugging.
 //echo '<br />'.$status.'<br />'.$username.'<br />'.$password.'<br />';
 post_to_facebook($status, $username, $password);
}
function post_to_facebook($status, $login_email, $login_pass)
{
 //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');
 $post_it = curl_exec($ch);
 if ($post_it)
 {
  //echo curl_errno($ch) . '-' . curl_error($ch);
  echo "<div class='msg'>Status Appears To Be Updated.</div>";
 }
 else
 {
  echo "<div class='msg'>Status Update Seems To Have Failed.</div>";
 }
 curl_setopt($ch, CURLOPT_POST, 1);
 //this gets the post_form_id value
 preg_match("/input type=\"hidden\" name=\"post_form_id\" value=\"(.*?)\"/", $post_it, $form_id);
 //we'll also need the exact name of the form processor page
 preg_match("/form action=\"(.*?)\"/", $post_it, $form_num);
 curl_setopt($ch, CURLOPT_POSTFIELDS, 'post_form_id=' . $form_id[1] . '&status=' . urlencode($status) . '&update=' . urlencode("Update status"));
 //set url to form processor page
 curl_setopt($ch, CURLOPT_URL, 'http://m.facebook.com' . $form_num[1]);
 curl_exec($ch);
 //close the connection
 curl_close($ch);
}
?>
<form method="post" action="index.php">
<label>Status : </label><textarea name="status" class="status"></textarea><br><br>
<label>Username : </label><input type="text" name="username"><br><br>
<label>Password : </label><input type="password" name="password"><br><br>
<div style="background-color:#ffcccc;" ><p style="color:#FF0000; font-size:10px;">Your login details are not stored! Feel safe.</p></div>
<input type="submit" value="Update" id="button">
</form>
</body>

CSS Code

body{font-family:'Lucida Grande', Verdana, sans-serif;; font-size:14px; color:#666666; width:500px;}
div.msg{background:#FFFFCC; margin-bottom:10px; padding:4px;}
input{border:1px solid #2BAE9E; color:#444444; padding:3px; font-family:Georgia, "Times New Roman", Times, serif; -moz-border-radius:6px;}
.status{border:1px solid #2BAE9E; color:#444444; padding:3px; font-family:Georgia, "Times New Roman", Times, serif; height:40px; width:400px; -moz-border-radius:6px;}
#button{background-color:#C8F0EF; border:2px solid #2BAE9E; padding:3px; -moz-border-radius:6px; float:right; margin:5px 5px 5px 5px;}

Written by

Arvind is a web developer, programmer and blogger. He has expertise in PHP, Magento, WordPress, jQuery, JavaScript, HTML5 and CSS3. He loves to develop good looking websites with strong backend.

8 comments :

  1. Your live demo is not working????

    ReplyDelete
  2. live demo not working as Nassir said

    ReplyDelete
  3. surely not working

    ReplyDelete
  4. your script does not work. can you fix it and tell me on my email at silvicone@yahoo.com ?

    ReplyDelete
  5. where is the index.php?

    ReplyDelete
  6. I like this script if it's working.

    ReplyDelete
  7. y its not working?Code is too good

    ReplyDelete

We would love to hear from you...

 

© 2014 Web Speaks . All rights resevered. Designed by Templateism