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!

Demo

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

Post to wall demo 1
Post to wall demo 2


Bob Belderbos

Software Developer, Pythonista, Data Geek, Student of Life. About me