Facebook profile box tips

As mentioned in my previous post, I’ve been finishing up a facebook application, My Sites. Now that it’s done, I thought I’d post a few tips on how to add profile boxes to your facebook application, since I found it maddeningly complicated.

For the non-facebook-savy, a profile box is a small box that applications can add to your profile’s “wall” or “boxes” tabs. They’re meant to provide a quick glimpse of what’s happening with the application.

I’m assuming that you keep a database of Facebook users, and you want to display something about them in the profile box.

The basic code for adding a profile box is below, annotated with copious comments. This is in PHP code, you can do it in other languages, but it’s what I use. Pop this code into a daily cron for example, and you’re good to go.

<?php
// This code connects to the facebook API.
@require_once 'facebook-platform/php/facebook.php';
$appapikey = 'facebook app api key';
$appsecret = 'facebook app api secret';
$facebook = new Facebook($appapikey, $appsecret);

// This code connects to your own database.
$BASEURL = "http://example.com/path/to/facebook/app";
$connection = @mysql_connect("server","username","password");
if(!$connection)die("Can't connect to the database at this time.");
$res = @mysql_select_db("database");
if(!$res)die("Site database doesn't seem to exist.");

// This code cycles through each user in the database
$sql="SELECT * FROM users";
$data=@mysql_query($sql);
while($user=mysql_fetch_assoc($data)) {
     // You can, of course, query your database to build these strings
     $WideBox = "Whatever you want a profile box on the 'boxes' tab to say.";
     $MobileBox = "Whatever you want a profile box accessed by a mobile device to say.";
     $NarrowBox = "Whatever you want a profile box on the 'wall' tab to say.";

     // This is the API call, it sets the user's profile box content:
     $facebook->api_client->profile_setFBML(
          NULL,        // This is the markup, set to NULL for 'FBML'
          $user['id'],  // ID number of the user whose profile box you want to set
          $WideBox,    // Wide box content for 'boxes' tab
          NULL,        // Deprecated, should always be NULL
          $MobileBox,  // Box content for mobile devices
          $NarrowBox   // Narrow box content for 'wall' tab
     );
}
// Clean up the connection
@mysql_close($connection);
?>
  • Share/Bookmark

Tags: , , ,

13 Responses to “Facebook profile box tips”

  1. James Ison says:

    Hey, Like the info but I have a question.
    I have created my own app for facebook which works as I want it to.
    With the above code, do I put this into a new php file? and if so, how do I call it so the profile box uses it??

    Thanks

  2. admin says:

    Hi James. What you want to do is to create a new text file on the server that hosts your facebook application, such as “profile_box.php”. Copy the code above into this file, make changes as needed (MySQL server, username, password; path to Facebook API, key, secret, etc.).

    Now you can generate the facebook profile box content by calling this PHP script. Three options for doing so:
    1. Visit the script in a web browser (httt://www.example.com/path/to/profile_box.php)
    2. Call the PHP script from a command-line prompt on your server
    3. Set up a CRON script to call the PHP script periodically (I did this — to update my facebook apps users’ boxes every day)

    Note that this just sets the facebook profile box content for your app users, in order to actually see it they must add the box to their wall/info tab.

    Hope this helps. Happy coding!

  3. James Ison says:

    Thanks for that, even if a little hard to read with the colours! :-)

    Is it not possible to have a separate php file for just the profile box and call it when the main app page is viewed and use the fbml_refreshRefUrl($url); function to refresh??
    I’ve have this but doesnt seem to be working as it should. Just getting ‘No content to display.’
    Running the profileinfo.php does give me what I want on it’s own, so I know that pages works.

    $url=”http://xx.xx.xx.xx/profileinfo.php”;
    $fbml = “”;
    $facebook->api_client->fbml_refreshRefUrl($url);
    $facebook->api_client->profile_setFBML(
    NULL, // This is the markup, set to NULL for ‘FBML’
    $user_id, // ID number of the user whose profile box you want to set
    $fbml, // Wide box content for ‘boxes’ tab
    NULL, // Deprecated, should always be NULL
    NULL, // Box content for mobile devices
    $fbml // Narrow box content for ‘wall’ tab
    );

    my profileinfo.php is a page that calls my sql database and pulls off a random app user and saved comment.
    Any problems with this that you can see?

  4. James Ison says:

    Seems to have lost some code in the posting – I’ll try adding spaces and \’s before the “’s
    $fbml = \” \” ;

  5. admin says:

    I think the problem is that you’re calling the “profileinfo.php” script

    $url=”http://xx.xx.xx.xx/profileinfo.php”;
    $facebook->api_client->fbml_refreshRefUrl($url);

    which sets the profile box content as you want, but then you immediately reset the content to the empty string

    $fbml = “”;
    $facebook->api_client->profile_setFBML(
    NULL, // This is the markup, set to NULL for ‘FBML’
    $user_id, // ID number of the user whose profile box you want to set
    $fbml, // Wide box content for ‘boxes’ tab
    NULL, // Deprecated, should always be NULL
    NULL, // Box content for mobile devices
    $fbml // Narrow box content for ‘wall’ tab
    );

    try eliminating the second piece of code. Also I think you could just do
    include(“profileinfo.php”);
    to call the script.

  6. lalfAnydayNap says:

    now in my rss reader

  7. Dawlalcoria says:

    yo, great name for site

  8. JaneRadriges says:

    Hi, very nice post. I have been wonder’n bout this issue,so thanks for posting

  9. Ehsan says:

    hi

    how can we update contents in profile box when client refresh browser

    thanks

  10. admin says:

    @Ehsan:
    That should be easy, just take out the while loop
    while($user=mysql_fetch_assoc($data))
    and use the current user’s id to generate the profile box content. Please note that it may take a while for the new content to propagate through facebook’s CDN.

    Hope this answers your question, if not post back here.

  11. Ehsan says:

    hi , thanks for this quick response , i miss my one line

    my content will update in flash or flex file (.swf) its possible to use upper code which is post by you.

  12. admin says:

    @Ehsan: not sure I understand your question… If you’re trying to post a flash object in the facebook profile box, I don’t think it will work – facebook restricts that content to HTML/FBML and images. If your trying to call the facebook API in flash/flex, I don’t know how to do that. The code above is in PHP. Maybe you can use flash interacting with JavaScript and FQL to do it.

  13. Scripto says:

    Excellent post..Keep them coming :) Thanks for sharing.

Leave a Reply