Example Facebook PHP SDK: Post to Friend’s wall

I got a question from one of my readers how to automatically post to the Wall of a friend. I started to code a quick example, but I realized it might be valuable to more people, so here is the code and a short howto.

featured image

This example is in PHP. You first create an app at the Facebook developer page, then you get the Facebook PHP SDK 3.0 and download it to the defined folder (localhost/whatever/). The complete code example is here

Here you can see how this looks :

Creating an app
Copy in the app codes

The SDK comes with an example php page, copy it over to the root of your project

Project structure
Login link
Login wrapper

For this exercise you need the extended permission publish_stream, which you append here:

Extended permission

Now when you login to the App you are asked for the normal and this extended permission:

Permission request

Now that your user is logged in you can get the friends with this bit of code:

Get friends

I embedded this in a form to choose the friend I want to message:

Design Form
Complete Form

The user selects his/her friend, writes a message / and/or puts in a link, and clicks submit. Index.php receives these 3 post variables and embeds them in the $facebook->api ‘post’ call.

Post to wall code

And that’s it. It seems much work, but by adding the SDK you can make it a much more powerful application. Needless to say, you need to validate the ouputs obtained through this form. This is beyond the scope of this example. I zipped the code here to play with it yourself. Don’t forget to first create your own App and put the App ID and Secret in the index.php page!


Here you see the form in action and the result on Roberto’s wall:

Post to wall demo 1
Post to wall demo 2

  • http://bobbelderbos.com/ Bob Belderbos

    Hi Martha, picture is a valid option (https://developers.facebook.com/docs/reference/api/post/), but I think the location of the image might influence – where do you host it?

    • http://bobbelderbos.com/ Bob Belderbos

      I posted about some restrictions here: http://bobbelderbos.com/2011/01/facebook-api-wall-post/

  • http://bobbelderbos.com/ Bob Belderbos

    make sure your app grants the stream_publish permission during authorization, see http://stackoverflow.com/questions/4158164/facebook-api-oauthexception-200

  • http://www.nischaltiwari.com Nischal Tiwari

    I want to post on Facebook page as well. Is there any ideas ?

  • Luccun

    thank you very much ! 

    • http://bobbelderbos.com/ Bob Belderbos

      you are welcome

  • Antonio Montani

    This is pretty interesting man!

    One question though, is it possible to post on your friends page without showing your name? Kind of like an App posting, with no “user” behind the post?

  • Januka

    Fatal error: Uncaught OAuthException: (#200) The user hasn’t authorized the application to perform this action thrown in/home/listudio/public_html/gift/fbsdk3/src/base_facebook.php on line 1028

    i got this error how can i fix this….

    • http://bobbelderbos.com/ Bob Belderbos

      that seems literally what it says: user did not authorize app. make sure test user confirmed permission box when logging into the app the first time

  • Alejandro Franco Conde

    I love this script but I’m having problems to make it post on a page, can you provide the necessary steps to make the form post on a page ? Thanks a lot

  • http://bobbelderbos.com/ Bob Belderbos

    you have to make your own fb app and put id and secret in the files provided. contact me in a personal message if you still have errors

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

    Thanks for sharing with us!

    • http://bobbelderbos.com/ Bob Belderbos


  • Aart de Bruijn

    Great post, thanks a lot!!

    • http://bobbelderbos.com/ Bob Belderbos

      you are welcome

  • Mohxinhayat

    hey!! which is the best free hosting plan for the facebook applications.. I have no credit card etc! can you just help me please!! i want a free hosting plan! n i have no domain! thnx 
    hope u’ll help me!

    • http://bobbelderbos.com/ Bob Belderbos

      not the place to ask, but you could just use heroku which facebook has integrated when you create an app

    • http://bobbelderbos.com/ Bob Belderbos

      not the place to ask, but you could just use heroku which facebook has integrated when you create an app

    • http://bobbelderbos.com/ Bob Belderbos

      not the place to ask, but you could just use heroku which facebook has integrated when you create an app

  • http://www.facebook.com/profile.php?id=517826655 Lina Fayez

    Great work :)..What if I want to make the same but with fan’s wall ! so I can post anything to fan’s wall..

  • Dav


  • Jason

    Great Article…Its work for me just fine..

    • http://bobbelderbos.com/ Bob Belderbos

      nice to to hear :)

  • http://bobbelderbos.com/ Bob Belderbos

    this script can send a message to one friend. I can imagine FB puts a limit for the amount of messages/friends you can send to prevent spam. I tested the script it works fine.

  • http://bobbelderbos.com/ Bob Belderbos

    “deemed abusive or is otherwise disallowed” – than you must have done something beyond the script’s use. I tested it today and it works just fine if you send 1 message to 1 friend

    • Tuan Basheer

      i also got this bloody error ;-(… wht shud i do?? please help me asap

  • http://bobbelderbos.com/ Bob Belderbos

     “request limit reached” – seems you are doing something else, making more calls to the api than this script

  • http://www.facebook.com/testernueve.tb Testernueve TB

    Hi. Excellent post. I would like to know if posting this on friends’ walls who have not installed the application is against Facebook policies and may be reported as spam or abuse. I am asking this because we are currently developing an app that invites friends to join by posts on their walls (in spite of sending invitations which are not effective at all). Thanks!

    • http://bobbelderbos.com/ Bob Belderbos

      posting to wall can be done manually with a dialog without permission. for an automatic way, you need to have the user granting your app the stream publish extended permission

  • http://www.mahfudcs.web.id/ Mahfud

     This is great post.. Make app to post on friend’s wall..

  • http://bobbelderbos.com/ Bob Belderbos

    check your php code

  • http://bobbelderbos.com/ Bob Belderbos

    contact me offline

    • http://www.facebook.com/profile.php?id=100003322405869 Thiago Lovatine

      Please, I have the same question…
      How can i write to more than one person?

      • http://bobbelderbos.com/ Bob Belderbos

        I will look into this … Did you search on Stackoverflow? I find a lot of FB API questions answered there lately

  • http://bobbelderbos.com/ Bob Belderbos

    please provide more details

  • http://www.facebook.com/akshay.vanjare Akshay Vanjare

     If friend of mine block me to post me on his wall, then is there any way to verify that is we have permission to post on friend wall?

  • http://www.facebook.com/neha.singhania.96 Neha Singhania

    I won’t able to run this code i changed the app id & app secret key but getting state and code params in the url and index.php showing Login with facebook even though i already logged in..
    Please Help me to trace the problem

  • Laura Designer

    Hi, is it working yet? Could you give me an update on this article?

© 2009-2015 - Bob Belderbos - All rights reserved.

- If you like something here, link to it instead of copy+paste.
- Disclaimer: ideas expressed on my blog are mine, and have nothing to do with the current/previous employers.
- Proudly using Wordpress and the Insider Theme on Bluehost