Facebook API: post with PHP

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.

  • http://twitter.com/fbleadgen fbleadgen

    Great stuff, alternatively you can also do this with a call to the Facebook Graph using CURL instead of the PHP SDK right?

    – DB, http://fbleadgen.com

  • liz

    hi bob, was just wondering where do we actually add these codes into?

    $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 “n”.$errors[‘message’];

    thank you for your attention..

    • Anonymous

      Hi Liz, wherever in your PHP code, where you have the variables to generate your automatic wall post ($review, $urlTitle, etc.).

      In my case, when a user reviews a movie @ sharemovi.es he / she needs to explicitly activate the wall post option so I fire off this code when this conditions is met:

      if(isset($postWall) && $postWall == ‘1’){


      • liz

        Thanks for the reply, Bob.

        Yes I went to ‘stackoverflow’ too at that time, but couldn’t figure it out just yet. But, I’m getting there.. hopefully.. :)

        In according to assigning a variable, did you mean something like the following, right?
        $review = “This is a sample line”;
        $urltitle = “http://www.bobbelderbos.com”;

        Hmm, how about assigning a variable from a database field?
        Will this correct?
        $review = $row[‘thereview’];
        $urltitle = $row[‘thereviewurl’];

        Sorry if the question isn’t really related to the topic.
        Thanks for the reply, Bob!

        • Anonymous

          see PM ..

    • Anonymous
  • http://www.facebook.com/krit.sasi Krit Sasithornwannakul


  • http://www.facebook.com/profile.php?id=100002074934937 Ajoydhas Amirtharaj


  • http://www.facebook.com/profile.php?id=100002074934937 Ajoydhas Amirtharaj


  • http://www.facebook.com/people/Nistor-Alexandru/1309600485 Nistor Alexandru


  • http://www.facebook.com/osama.salamaa Osama Salama


    • Anonymous


  • http://www.facebook.com/osama.salamaa Osama Salama


  • http://twitter.com/JuiEnsoi Cariza Joy

    Hello! Thanks for sharing, very helpful :D
    may I know how to implement the “Post to my fb wall?” part??

    • http://bobbelderbos.com/ Bob Belderbos

      contact me off thread if you need more help

© 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