A few weeks ago I wrote a blog post on how to tweet with PHP. The post was quite successful so today part II: Facebook API: post with PHP. The principle is the same: you need to register an app on Facebook and with the API key you can get started.

First you go to Facebook's developer page and add an app. You can name it anything you want but make sure you put the right URL of the site (if you develop on your localhost you might consider register two apps because one api key is linked to one URL)

Then you download the facebook php client library and put in the application key and secret. Then you can use the Facebook login button to have people login to your app.

I am flying over this here because the main thing I wanted to show you today is how to automatically post to your Facebook wall. I will show you an example how I implemented this on sharemovi.es, a site I built to bring together movie fans.

The post that really helped me doing this was on stackoverflow which is one of those resources that always show up in Google, but also provides you with good answers!

Once you have initiated the facebook instance with the php client library, posting to facebook is quite easy:

$attachment = array('message' => 'Check out this movie ..',
 'name' => $title,
 'link' => 'http://sharemovi.es/' . $urlTitle,
 'description' => 'Review: ' . $review,
 'picture' => $picLocal
 );
  if(!($sendMessage = $facebook->api('/me/feed/','post',$attachment))){
  $errors= error_get_last();
  echo "Facebook publish error: ".$errors['type'];
  echo "<br />\n".$errors['message'];
 }

Unfortunately Facebook does not allow you to select a FBCDN image (FBCDN standing for "Facebook content delivery network"), so you have to use another one or copy one from the FBCDN over to your server (hence $picLocal)

The beauty of the Facebook API lays in the fact it is highly customizable. As you can see, you can exactly specify what the wall post should look like, in my case a link to the new movie title, the review submitted by the user, etc.

Now to understand how this works for the user, let's see a practical example: a user logs into sharemovi.es via the Facebook login button. The first time the user is asked by Facebook to confirm if the Facebook app/ sharemovi.es may access his/her personal info, email and since last week the extended permission to post to his/her wall. Below you see only the latter permission, because the basic permissions I accepted already.

This allows the app to post to the user's wall, but that is only possible when a valid Facebook session has been established (user has logged in).

Implementing this technique, I made the design choice to explicitly ask the user to use this feature. This way there shouldn't be any misunderstanding when sharemovi.es posts to your wall.

Finally here you can see the outcome: a nice customized wall post!

You will recognize the fields of the code snippet. Friends can like and comment on your post, just as any link you share with the existing Facebook buttons. Below the post you see the source, which is the application, Sharemovi.es in this case.

One final thing, you can easily delete the post and even, in the same menu, revoke all access the Facebook application has been given. Facebook thought this out well: your granted access is never permanent and as easy as you give it, as easy it can be taken away:

I hope you liked this post. If so, please use the social media buttons at the top and the bottom of this post to spread the word. You can also subscribe to this blog (see sidebar) to receive a periodic newsletter with my latest blog posts.


Bob Belderbos

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