David Turner

Facebook Query Language

I've been spending some time over the last couple of days wrestling with the various aspects of Facebook with regards to creating something that actually works and displays that most precious of Facebook related stuff... data. I thought I'd take some time to share the pieces of code I've come up with that have helped me in what is proving to be a frustrating coding exercise.

Starting Point - PHP

I'll be honest, I'm a big fan of both PHP and JavaScript but so far I've only really found PHP to be of any real help to me. This is because, as I'm sure you're aware, PHP is a server-side scripting language, as opposed to a client-side one. This has a couple of advantages to it:

  1. Can't be turned off by the user
  2. I can opt to store some data in a database and access it instead of making external calls
  3. Server-side script can be used to dynamically generate client-side code, the reverse is not true.

As a result, I've been finding myself wrestling with various bits of PHP. This started off with throwing queries to Facebook and getting back JSON formatted results. This actually worked pretty easily but, because of all the parsing required it was also very slow. This was less than ideal, and why I'm not sharing the code I used with it for you. Shortly thereafter, I started to torture myself with FQL - Facebook Query Language.

Facebook Query Language

I'll be honest, Facebook's query language really confused me at first... it still does if I'm perfectly honest. I've only just started dabbling in it and, as a consequence, don't know all the ins and outs of what goes on, or how to best go about making stuff happen. Things started out quite simply, as there are only a few pieces of data that I really need for the starting point I have for my app. These pieces of data are:

  • List of logged in user's friends
    • ID
    • Name
  • List of friends of logged in user's friends

To start, we need to create and register our app with Facebook. If you haven't already done so, you really should head on over and get yourself set up. Doing so will give you the information you need to even be able to try and make any Facebook magic happen. All set? Sweet!

Facebook offered me an example I could use to help get started with things (available here by downloading the example code for your app). It gave me a few bits of code as well as the PHP-SDK which allows you to access Facebook's data using PHP. In the code I'm displaying today, there's a lot of this that has been stripped away (some of which is actually needed to be able to let new users access your app), but I'm here to focus on getting data in, not setting up the app.

So, with that said, there are a few snippits of code that I found have been awesome in pulling in data, based around FQL and PHP parsing of it. The first is:

    
      $fql = "SELECT uid2 FROM friend WHERE uid1=" . $uid;
      $param = array(
       'method' => 'fql.query',
       'query' => $fql,
       'callback' => ''
      );                                                           
      $fqlResult = $facebook->api($param); 
    
  

This piece of code does one thing. It pulls in a list of people with whom the logged in user (you) is friends with. This returns a rather vast array of data. To convert it into something a little but more useable we can, once again, turn to PHP and a foreach loop:

          
      //test
      foreach($fqlResult as $result){   
        echo $result['uid2']."<br />";
      }
    
  

This will loop through all the pieces of the array and echo out the uid2 elements from the array. This will result in a huge list of numbers being displayed down your screen. Each of them is a user ID for a friend. Printing them is just an easy way to show that data is being retrieved. You could, instead, store it in a variable or, if you want something a bit more permanent, in a database that you can retrieve data from in the future.

I've not progressed much past this point myself, but I thought that I'd share the information that I've come up with for others to look over. You can use it if you want, or ridicule me for how sloppy it is. Your call. I know I'm not the only person struggling to wrap their head around making Facebook play nicely, so it is my hope that this information proves useful. If anyone reading this has a better way to implement it, by all means share it too. I know that I'd appreciate it, even if others don't.

Making Life Easier (Update)

A certain Kyle Gawley mentioned on twitter another way to list user id and that user's name. As it's a lot easier to implement than the method I've covered above I asked if it was ok for me to steal his example and add it to this blog post. Given that you're reading this, I've either been given his permission or I'm blatantly stealing his code. I'll let you guess as to which one is the truth. Onto the code:

    
      $friends = $facebook->api('/me/friends');
    
  

Because the logged in user is always the person viewing the page, you can run a much simpler query using the above code, which looks at the users which are the current user's friends. This returns an array of data. If you were to use something like print_r($friends); you would see a rather lengthy list of user ids and names wrapped up in all sorts of magical arrays.

So now we're getting data... more data than with the previous query, and plenty to start playing around with. The question now, however, is how do we get it laid out a little bit better? The answer is by turning to our good old friend the foreach loop:

    
      foreach ($friends['data'] as $friend) {
        echo $friend['name'].' '.$friend['id']."<br />";
      }
    
  

This time it's a little bit different, as the returned information is stored inside an array item labelled data, stored inside the variable . We get this information passed into the foreach loop by calling it as $friends['data'] which we can then loop through. The echoed out code will display both the person's name and their user id. If you found this piece of code useful I'm sure Kyle wouldn't object to being praised and adored on the twitterwebs.