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.
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 :
The SDK comes with an example php page, copy it over to the root of your project
For this exercise you need the extended permission publish_stream, which you append here:
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!
Here you see the form in action and the result on Roberto's wall: