Warning: Creating default object from empty value in /home/ibrfao/masteringapi.com/blog/wp-content/themes/platform/includes/class.layout.php on line 164

Warning: Creating default object from empty value in /home/ibrfao/masteringapi.com/blog/wp-content/themes/platform/includes/class.layout.php on line 167

Warning: Creating default object from empty value in /home/ibrfao/masteringapi.com/blog/wp-content/themes/platform/includes/class.layout.php on line 170

Warning: Creating default object from empty value in /home/ibrfao/masteringapi.com/blog/wp-content/themes/platform/includes/class.layout.php on line 173

Warning: Creating default object from empty value in /home/ibrfao/masteringapi.com/blog/wp-content/themes/platform/includes/class.layout.php on line 176

Warning: Creating default object from empty value in /home/ibrfao/masteringapi.com/blog/wp-content/themes/platform/includes/class.layout.php on line 178

Warning: Creating default object from empty value in /home/ibrfao/masteringapi.com/blog/wp-content/themes/platform/includes/class.layout.php on line 180

Warning: Creating default object from empty value in /home/ibrfao/masteringapi.com/blog/wp-content/themes/platform/includes/class.layout.php on line 202

Warning: Creating default object from empty value in /home/ibrfao/masteringapi.com/blog/wp-content/themes/platform/includes/class.layout.php on line 206

Warning: Creating default object from empty value in /home/ibrfao/masteringapi.com/blog/wp-content/themes/platform/includes/class.layout.php on line 224

Warning: Creating default object from empty value in /home/ibrfao/masteringapi.com/blog/wp-content/themes/platform/includes/class.layout.php on line 225

Warning: Creating default object from empty value in /home/ibrfao/masteringapi.com/blog/wp-content/themes/platform/includes/class.layout.php on line 227

Warning: Creating default object from empty value in /home/ibrfao/masteringapi.com/blog/wp-content/themes/platform/includes/class.layout.php on line 321

Warning: Creating default object from empty value in /home/ibrfao/masteringapi.com/blog/wp-content/themes/platform/includes/class.layout.php on line 321

Warning: Creating default object from empty value in /home/ibrfao/masteringapi.com/blog/wp-content/themes/platform/includes/class.layout.php on line 321

Warning: Creating default object from empty value in /home/ibrfao/masteringapi.com/blog/wp-content/themes/platform/includes/class.layout.php on line 321

Warning: Creating default object from empty value in /home/ibrfao/masteringapi.com/blog/wp-content/themes/platform/admin/class.options.metapanel.php on line 56

Warning: Creating default object from empty value in /home/ibrfao/masteringapi.com/blog/wp-content/themes/platform/admin/class.options.metapanel.php on line 56

Warning: Creating default object from empty value in /home/ibrfao/masteringapi.com/blog/wp-content/themes/platform/admin/class.options.metapanel.php on line 56

Warning: Creating default object from empty value in /home/ibrfao/masteringapi.com/blog/wp-content/themes/platform/admin/class.options.metapanel.php on line 49
How To: Post A Message On The User Wall Using Facebook Graph API | MasteringAPI

This is a quick post to demonstrate how to post a status message on the user wall using the Facebook Graph API. First of all, you need the publish_stream extended permission to perform this action.

 

Using the PHP-SDK

$args = array(
	'message'	=> 'Hello from my App!',
	'link'		=> 'http://www.masteringapi.com/',
	'caption'	=> 'Visit MasteringAPI.com For Facebook API Tutorials!'
);
$post_id = $facebook->api("/me/feed", "post", $args);

Posting a message and a linkPosting a message and a link

In the above example we posted a message and a link using the PHP-SDK.

Back to top
 

Using the JavaScript-SDK

This is an example of posting a message using the JavaScript SDK taken from the FB.api documentation:

var body = 'Reading Connect JS documentation';
FB.api('/me/feed', 'post', { message: body }, function(response) {
  if (!response || response.error) {
    alert('Error occured');
  } else {
    alert('Post ID: ' + response.id);
  }
});
Back to top
 

Notes

Please note that you can still post to a user wall even if the user is not connected to your application and without offline_access permission just by knowing the user id:

publish_stream:
Enables your application to post content, comments, and likes to a user’s stream and to the streams of the user’s friends. With this permission, you can publish content to a user’s feed at any time, without requiring offline_access. However, please note that Facebook recommends a user-initiated sharing model.

$args = array(
	'message'	=> 'Hello from app',
	'link'		=> 'http://www.masteringapi.com/',
	'caption'	=> 'Visit MasteringAPI.com For Facebook API Tutorials!'
);
$post_id = $facebook->api("/$USER_ID/feed", "post", $args);
Back to top
  • Nagendra1612

    Thanks a lot…..
    i searched in google and cant find it….
    your post helped me a lot..

    keep posting….

    • http://www.masteringapi.com Ibrahim Faour

      You are welcome!

      • guest

        hey guys, sorry for the really dumb question, but how do i go about granting the publish_stream permission for my app?

        • CC

          Your not dumb. Its the API and documentation that is stupid

          • Wends Ang

            just tried this code.. didn’t return an error but it only publishes to users that i register as testers of the app. is there anything i can do to the app before my message gets reflected on friends’ walls? 

        • dbind

          The permissions you require are set right in the call that makes the user log in using his facebook account. The parameter is either scope or perms (depending on which method you’re using). The oauth2 method, which is recommended, uses scope. If you already have code to get started, you’ll probably see “scope=email” or “perms=email”. Just add all permissions you think you’ll need, separated by comma, i.e. “scope=email,publish_stream”.

  • nakres

    hi using your below code,
    how can i have
    3 or 4 lines of text and image on the left side of the message and link back to the application?
    thank you

    var body = ‘Reading Connect JS documentation’;
    FB.api(‘/me/feed’, ‘post’, { message: body }, function(response) {
    if (!response || response.error) {
    alert(‘Error occured’);
    } else {
    alert(‘Post ID: ‘ + response.id);
    }
    });

    • http://www.masteringapi.com Ibrahim Faour

      Something like this would get you started:
      var body = 'Line onenLine Two';
      var url = 'http://www.masteringapi.com/';
      var caption = 'My website!';
      var picture = 'http://domain.com/path/to/picture.jpg'
      FB.api('/me/feed', 'post', { message: body, link: url, caption: caption, picture: picture }, function(response) {
      if (!response || response.error) {
      alert('Error occured');
      } else {
      alert('Post ID: ' + response.id);
      }
      });

      • nakres

        thank you very much for your reply
        i will try it tonight

        1. would be image on the left to the text?

        2. is that how the lines go?
        var body = ‘Line onenLine TwonLine three’;

        3. would it be also posible when they click on the image it will take them to the web site?

        4.and also would it be possible to say
        my website but when they click it will take them to my website without actually showing the link?

        many thanks again

        • http://www.masteringapi.com Ibrahim Faour

          1. I’m not sure, but yeah I guess this is the default behavior of Facebook
          2. Yes
          3. I’m not sure if Facebook will link the image to the link parameter but if not I doubt you can control this
          4. No, you can only set the parameters Facebook provide and you have to accept how they deal (display) them :-)

          • nakres

            yes it worked very nicely, thank you very much
            i have to 3 things
            how can i add a second line of caption?
            when i put my url i see the path to my facebook canvas adress but when i put yours i see your page sites title which i prefer to do but dont know how to?
            i saw some applications add link right next to comment
            could i do the same
            it is like
            Like . Comment . Connect (with to the application link)

            many many thanks

          • nakres

            also how can add users name in the caption or body?

            thank you

          • http://www.masteringapi.com Ibrahim Faour

            You need to make a call to /me first to get the user basic info (and the name), this example is from the documentation:
            FB.api('/me', function(response) {
            alert(response.name);
            });

          • http://www.masteringapi.com Ibrahim Faour

            1)I’ve never tried it, but if it didn’t work like the message parameter it wont work
            2)Try using the title tag and also the Open Graph meta tags
            3)This should be added by default

          • Shivi_man80

            pls provide me code of how to send link from java application to facebook wall

  • http://profiles.google.com/tom.maiaroto Thomas Maiaroto

    Oddly enough I can’t post with a link parameter using the PHP SDK. I’ve included the link, caption, and description keys just in case (read elsewhere that you need a description with a link) … But it still won’t post to the friend’s wall. I CAN, however, post with just a message and that works just fine. It’s only when I include a ‘link’ key that it doesn’t post. Weird. Any ideas? Thanks and great site!

    • http://www.masteringapi.com Ibrahim Faour

      1- Try the same code that I posted here
      2- If it didn’t work try posting it on your own wall, if it worked then it might be some sort of Privacy setting your friend added.

  • Pure Vintage Skt

    can i post on the walls of the friends of the user, how so?

    • http://www.masteringapi.com Ibrahim Faour

      You just replace the me or the user id with the friend’s id

  • Nakres

    hi,
    how can i autopost on the user wall if i have the permission for publish_stream?

    thank you

  • Nakres

    }
    function APICalling(){
    var body = ‘TextnText’;
    var url = ‘http://apps.facebook.com/application’;
    var caption = ‘Text’;
    var description = ”;
    var name = “Text”;
    var picture = ‘…..pic.jpg’
    FB.api(‘/me/feed’, ‘post’, { message: body, link: url, caption: caption, picture: picture, description: description, name: name }, function(response) {
    if (!response || response.error) {
    // alert(‘Error occured’);
    } else {
    // alert(‘Post ID: ‘ + response.id);
    }
    });

    above code is with some other code in one file
    and some other file has the below to triger it

    the problem is IE
    it works perfect with FF
    but with IE it does not work

    could you please help me?

    also

    how would i add this code to my codes? and how would i triger that with onload?

    $args = array( ‘message’ => ‘Hello from app’, ‘link’ => ‘http://www.masteringapi.com/’, ‘caption’ => ‘Visit MasteringAPI.com For Facebook API Tutorials!’); $post_id = $facebook->api(“/$USER_ID/feed”, “post”, $args);

    Thank you

    • nakres

      i have this, i tried , i tried and i tried, FF has no problem it tells me
      ‘Post ID: ‘ + response.id
      but now IE is saying ‘Error occured’

      function updating(){
      var body = ‘YuhuYuhu Kameralı Sohbet Servisin24 Saat Online Kameralı Sohbet’;
      var url = ‘http://apps.facebook.com/yuhuyuhu’;
      var caption = ‘YuhuYuhu 24 Saat Online Kameralı Sohbet’;
      var description = ‘ ‘;
      var name = “Facebook YuhuYuhu Kameralı ‘ ya BAĞLAN”;
      var picture = ‘http://mysite.com/webcam8.jpg’;
      FB.api(‘/me/feed’, ‘post’, { message: body, link: url, caption: caption, picture: picture, description: description, name: name }, function(response) {
      if (!response || response.error) {
      alert(‘Error occured’);
      } else {
      alert(‘Post ID: ‘ + response.id);
      }
      });
      }

      could you please help me

  • Anonymous

    Great, thank you, you’re master

  • Wck

    I have try it, but it shows:
    Uncaught OAuthException: (#200) The user hasn’t authorized the application to perform this action thrown in /XXX/

    so what can i do?

    • http://www.masteringapi.com Ibrahim Faour

      Make sure you have granted the publish_stream permission.

  • Carolyn

    Thanks for your tutorial.
    Is there any way to hide the application’s name that below the post?
    I mean there’s one line “9 seconds ago via fblocal” below your post,
    which fblocal is your application.
    Yes indeed, I need to post this message via my application….
    but, is there any way to post AS page/user (not post TO page)???

  • Katti Shyam

    How can I post it on a facebook page that I administer? i.e. instead of a user’s wall, how do I update Facebook page?

    Thanks a lot for your code.

    -Shyam

  • Martin

    Hi, I have this all working but how would I go about it if I wanted the post to show up in the peoples news feed like a status update and not just post to the users wall?

    Thanks for your help

  • Vayu Sen

    Hello,

    Unable topost by using above PHP script , could you provide complete PHP code where I can use in my existing code to post with access_token…Thanks.

  • Kiran BS

    Awesome posts!

    Can you tell me how to post photo on friends wall. Not as a feed.

  • Puremillions

    SO, does this code post to a users wall as the user or would it be like me posting a link on the users wall?

    • http://www.masteringapi.com Ibrahim Faour

      You as “developer” has nothing to do with it. So if a user X authorized your app and you initiated the above call: $facebook->api("/me/feed", "post", $args);
      User X will be the one posting. Obviously, you can post someones else if they didn’t authorize your app. So user X can post to his friend’s wall, but NOT as his friend!

  • Abc

    Thanks buddy its really…. Saved lot of my time……………

  • CC

    Why cant the post in this example be shared?

  • Wends Ang

    just tried this code.. didn’t return an error (actually returns post_id’s) but it only publishes to users that i register as testers of the app. is there anything i can do to the app before my message gets reflected on friends’ walls? 

  • http://www.facebook.com/people/Dionisis-Katsibas/1201230359 Dionisis Katsibas

    hi, nice tutorials.
    My question/problem is that in the post does not appear an image. My code is exactly with yours (“using the php-sdk”) . the only change is that i have put  another link (link of a facebook page tab). What can i do? In addition, can i put my own picture and how? thank you very much

  • Alessio Somma

    thanks man, nice post!
    by the way how can I post an application action for a specific object with php sdk?
    maybe something like this $fb->api(“me/application_namespace:action”, “post”, $args) ?

  • Jaylenwatkins

    This is good work done by you dude.  It helps a lot.

    http://www.graphs.net

  • Praveen

    This is good link for commona and like in facebook 

  • http://www.facebook.com/tantaikooi Glyn Tan Tai Kooi

    nice

  • http://www.r-gate.net/ Mohamed Tair

    Thanks buddy its really…. Saved lot of my time……………

  • Prakash Malaviya

    Hi, this is works for me and even i was also get id in reply but i can’t see message on my wall

    • http://www.facebook.com/true.mohsin Mohsin Khan

      This is test comment

  • http://www.facebook.com/eric.dcmd Eric Dcmd

    Thanks for the great help.

     I have no idea why this information is so hard to find when using the official Facebook documentation.

  • http://www.facebook.com/danideu Daniel Deudero

    Hi,

    Thanks for this tutorials, is amazing
    In my app, I work with PHP SDK and I post a message in a friend wall but dont recieve Notifications. How can i send post with Notifications?

    Thanks!..

  • http://www.facebook.com/profile.php?id=100005051850734 Ganesh Kumar

    How to do the same operation in IOS SDK?.

  • http://www.facebook.com/Harshalpatil22 Harshal Patil

    this is test comment

  • sandy

    i have used “email,user_birthday,status_update,publish_stream,read_stream,offline_access”

    all these permission still get error.

    Fatal error: Uncaught OAuthException: (#200) Feed story publishing to other users is disabled for this application thrown in /var/www/html/dev/lib/pear/facebook-api/base_facebook.php on line 1043

    Please help me whts wrong in it

    • Joe

      This code just doesn’t work anymore. Facebook changed the API.

  • Deval

    I am getting the following error : -

    Fatal error: Uncaught OAuthException: (#1500) The url you supplied is invalid
    thrown in /home/a1395850/public_html/src/base_facebook.php on line 1254.
    Please help me…

  • Priya Pagal

    My dog writes better API than FB fools. Never reliable even after CallBack shows success with proper user/post IDs. Everyday fools create new frustration for the community