Thursday, April 21, 2011

Posting to Facebook's friend wall - PHP


Hi,

This post is again a continuation of my previous posts related to Facebook Graph API. This one will sum up all the earlier post with a new functionality of posting to your own or friends wall. In this also I will be using the $facebook->api() to post in wall. Click here to download the entire source code.

My current page looks like:

In the above I have a form for updating my Facebook status. When I click the image of my friends a popup comes up and lets me post to that fiends wall as follows:



Code and explanation:


First I am creating the following form for submitting the post:


<form method="post" action="">
<input type="hidden" name="user" value="FACEBOOK_USER_ID">
<textarea name="status" style="border:1px solid #ccc;width:289px" rows="4"></textarea><br/><br/>
<input type="submit" value="Post in Wall">
</form>

Now when the form is submitted, I am using the $facebook->api() function to post in wall as:


if (isset($_POST['status']))
{
try {
$statusUpdate = $facebook->api('/'.$_POST['user'].'/feed', 'post', array('message'=> $_POST['status'], 'cb' => ''));
} catch (FacebookApiException $e) {
echo "<div style='color:red'>Error!! Status update failed.</div>";
}
if(!empty($statusUpdate)){
echo "<div style='color:green'>Congratulations!! Your facebook status has been successfully udpated.</div>";
}
}
}

In the above:
 $facebook->api('/'.$_POST['user'].'/feed', 'post', array('message'=> $_POST['status'], 'cb' => ''));

is the syntax for sending post, where $_POST['user'] is the Facebook user ID and $_POST['status'] is the status message.

Download: fb_wall_post.zip

After downloading the source code, you just need to change the API key and secret key, which you can generate for your website from https://developers.facebook.com/setup/

Cheers!!!

13 comments:

  1. I like this, but it doesn't work for me :(
    When i click, post button, go the screen http 500 error...

    what mistakes?

    thanks the answer!

    ReplyDelete
  2. Are you using correct facebook api keys? because there are all working codes. I will still double check and get back to you, in case facebook has changed any of its API's

    ReplyDelete
  3. thanks for the reply, it still failed, but keep trying ;)
    everything is fine in principle

    ReplyDelete
  4. Fatal error: Call to undefined method Facebook::getSession()

    help me please for solutions

    ReplyDelete
  5. Can We Make It Like

    It Will Post On All Friends Wall In Just One Go ?

    ReplyDelete
  6. good work dear....
    thanks u so much....
    its working....

    ReplyDelete
    Replies
    1. but not working for one person.

      Delete
  7. Thanks for posting It's working fin..

    ReplyDelete
  8. Facebook.FacebookOAuthException: (OAuthException - #200) (#200) Feed story publishing to other users is disabled for this application- i m getting above error

    ReplyDelete
  9. Hello,

    I've got the same message :

    "Feed story publishing to other users is disabled for this application- i m getting above error"

    ReplyDelete
  10. in source code line 4 gives an error.. i thnk in session

    ReplyDelete
  11. Hello,

    this code is not working fine for me. earlier it was giving this erro messages:
    Notice: Undefined variable: uid in D:\wamp\www\testpages\fb_wall_post\example.php on line 21

    Fatal error: Uncaught CurlException: 60: SSL certificate problem, verify that the CA cert is OK. Details: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed thrown in D:\wamp\www\testpages\fb_wall_post\src\facebook.php on line 640

    Then i comment these lines but still its not working properly.

    http://my.xportsoft.com/fb_wall_post/example.php

    pls check this.

    ReplyDelete