Hi there!

A few days ago, I started to write my first own facebook application. It all went quite okay until one certain point: I wanted my application to update the profile box of the user, which uses the app at that single moment. And that wasn’t as easy, as I thought.

The problem was: after the profile box has been updated, all profile boxes of all users had the same content – the content of the user who last updated his profile box.

I will come to the solution later. First of all, a quick explanation of what I needed to do:

  1. I read the documentation of facebook’s application guide to understand the API and the FBML language
  2. I set up my own application using the step-by-step guide
  3. I set up my new application on my own host including facebook’s PHP libraries

Here’s the code step-by-step (just put all sniplets together):

Code sniplet #1

require_once 'facebook-platform/php/facebook.php';

$appid = 'xxxxxxxxxxxx';
$appapikey = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$appsecret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';

$facebook = new Facebook($appapikey, $appsecret);
$user_id = $facebook->require_login();

Above: first of all, we need to include facebook’s PHP libraries, define several variables for our application, initialize our facebook object and retrieve the UID of the user who currently uses the application.

Code sniplet #2

$fb_box = "<H4 class='box_header clearfix'><span>";
$fb_box .= "Hello World!";
$fb_box .= "</span></H4>";
$fb_box .= "<div>I am ";
$fb_box .= "<fb:name uid=\"$user_id\" useyou=\"false\" />.";
$fb_box .= "Nice you're on my page.</div>";
$fb_box .= "Some more content in HTML or FBML...";

Above: we then define the content which we want to be displayed within the profile box. In this examble, it will be the name of the user, greeting the visitor.

Code sniplet #3

$fb_box_handle = 'pb_' . $appid . '_' . $user_id;

Above: this is important! At this point, I got stuck for a while. The reason was, facebook caches profile boxes content until the application’s canvas page is requested for the next time. The handle is very important for that caching mechanism, since it has to be a unique identifier for each application and each user. For that reason, I simply use the prefix ‘pb_‘ followed by the applications ID (defined above) and the UID of the current user (also defined above)!

Code sniplet #4

$facebook->api_client->call_method('facebook.profile.setFBML',
array(
'api_key' => $appapikey,
'v' => '1.0',
'uid' => $user_id,
'profile' => '<fb:narrow><fb:ref handle="' . $fb_box_handle . '" /></fb:narrow>',
'profile_main' => '<fb:ref handle="' . $fb_box_handle . '" />',
)
);

Above: the rest is quite simple. We make an API call to facebook and send our application key, the protocol version, the UID of the facebook user whose profile box ought to be updated and, of course, the content for that box. The content will be sent as the reference handle we defined earlier.

If you want to update multiple profile boxes with one single API call, I guess, this could work if you send an array containing the UIDs. If not, you can still get all those UIDs, walk through the array and perform multiple API calls. I read somewhere, some users had problem updating large numbers of profiles… Just give it a try.

Comments (if it worked for you or even if it didn’t) are appreciated 🙂

UPDATE because of comment #2:

The example above is about updating content in a users profile, not about setting up a profile box on a profile. You should read the manual carefully.

A user has to approve a certain application to create a profile box. Therefore, you need to render a button, which sets up the box. You can easily do this by:

$appapikey = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$appsecret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$facebook = new Facebook($appapikey, $appsecret);
$user_id = $facebook->require_login();
$facebook->api_client->profile_setFBML($appapikey, $user_id, 'profile', NULL, 'mobile_profile', 'profile_main');
echo "<fb:add-section-button section='profile' />";

The last line renders the button, you want to have on your canvas page.

Good luck! Thomas

Hat’s gefallen?

Wenn der Artikel gefallen hat oder hilfreich war, bitte folgende Funktionen benutzen:

Tweet
  • Facebook Platform Team replied to this documentation on the developer forum with:

    — snip —
    Hi Thomas,

    Thanks for this helpful guide. We’ve added it to our Offsite Tools and Tutorials post.

    Cheers
    — snap —

    Nice 🙂

  • 212:

    hello,
    please, i have juste developped my facebook application
    http://apps.facebook.com/jeu-megane-algerie/
    i want to add texte to user profil i try your code but it doesn’t work.
    i dont khow why.
    can you help my please ?
    thank you lot of
    sorry i don’t speak english well.

  • Dear 212!

    Please see the update of my explanations. You obviously forgot to render a button, which creates the profile box. My example just explained, how to update a box, not how to create a box!

    Good luck!

    Thomas

  • Great and useful – Thank you.
    When the box is added to the ‘box’ tab instead of the profile, it seems not get filled properly. Would you kindly direct me where to find info to figure that out – if you know.

  • Ankit:

    Hi Thomas…Your code worked perfactly but it was just a static text ‘profile’ in profile box. Can you please give some more to let me know how to display the content from some url. I’ve already used fb:ref but it didn’t work. So can you just show some code with which I can compare mine.

    Thanks.

  • Ankit,

    did you try the code exactely as I posted? Still not working? Please provide your code.

    Thomas

  • Ankit:

    <?php
    require_once ‘facebook.php’;

    $appapikey = ‘********************************’;
    $appsecret = ‘********************************’;

    $facebook = new Facebook($appapikey, $appsecret);

    $fb_box = “”;
    $fb_box .= “Hello World!”;
    $fb_box .= “”;
    $fb_box .= “I am “;
    $fb_box .= “.”;
    $fb_box .= “Nice you’re on my page.”;
    $fb_box .= “Some more content in HTML or FBML…”;

    $fb_box_handle = ‘pb_’ . ***********(my application id) . ‘_’ . ***********(page id);

    $facebook->api_client->call_method(‘facebook.profile.setFBML’,
    array(
    ‘api_key’ => $appapikey,
    ‘v’ => ‘1.0’,
    ‘uid’ => ***********,
    ‘profile’ => ”,
    ‘profile_main’ => ”,
    )
    );
    $fbml = “”;

    $facebook->api_client->profile_setFBML($appapikey, ***********(page id again), $fbml, NULL, NULL, ‘profile_main’);
    ?>

    Since I am a neophyte to this I don’t know which code of above is significant & which one is redundant, so I did copy-paste everything you posted with fb:ref addded.It works with static text but not in case of url.Thanks for the quick reply.

  • Ankit:

    I want to mention that I’ve used $fbml=fb:ref but it has been replaced with ‘0’ in the post & second thing Im using page id instead of userid…so I think you can definately find the problem from all this…

  • Osku:

    I don’t really get it. You claim you send in the content for you’re profile box, but you really don’t. You merely create you’re content into $fb_box, but the code to be displayed on the profile box only contains the “handle”-code. You never really set the handle. Am I right?

  • alex:

    I presume it is safe to say that for many people the buttons are not working becaues they have not updated to the latest facebook library
    get it here:
    http://developers.facebook.com/get_started.php
    I was using the one that comes with smiley and it does not work with profile boxes.

  • Great help, thanks. I just have one hangup. I can’t get the HTML in fb_box to show up. I get a profile box but it says “no content to display”. Is it that $fb_box isn’t referenced anywhere in the file or is fb_box_handle supposed to render that?

    Here’s what I’m using:

    require_login();
    $facebook->api_client->profile_setFBML($appapikey, $user_id, ‘profile’, NULL, ‘mobile_profile’, ‘profile_main’);

    $fb_box = “”;
    $fb_box .= “Hello World!”;
    $fb_box .= “”;
    $fb_box .= “I am “;
    $fb_box .= “.”;
    $fb_box .= “Nice you’re on my page.”;
    $fb_box .= “Some more content in HTML or FBML…”;

    $fb_box_handle = ‘pb_’ . $appid . ‘_’ . $user_id;

    $facebook->api_client->call_method(‘facebook.profile.setFBML’,
    array(
    ‘api_key’ => $appapikey,
    ‘v’ => ‘1.0’,
    ‘uid’ => $user_id,
    ‘profile’ => ”,
    ‘profile_main’ => ”,
    )
    );

    echo “”;
    ?>

  • Ack, figured it out! Different question, though: would you happen to have a tutorial or know of one that would allow this to be added to either a profile or a page?

    Thanks for the help.

  • I am so sorry for all of you asking me questions about this topic, because I really don’t have enough time to check out all your code and problems and answer all of you…

    I will anser as soon as possible, if still needed.

    Thomas

Leave a Reply