Example Facebook PHP SDK: Post to Friend's wall

August
21st,
2011

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.

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

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

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

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.

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!