<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>
<channel>
	<title>University Logs of David Turner</title>
	<atom:link href="http://uni.davidturner.name/feed/533/" rel="self" type="application/rss+xml" />
	<link>http://uni.davidturner.name/</link>    
	<description>Collection of posts from my IMD Learning Logs</description>
	<language>en</language>
	<sy:updatePeriod>daily</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>



<item>
	<title>Facebook Query Language</title>
	<link>http://uni.davidturner.name/533/05.1.-Facebook-Query-Language/post/</link>
	<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate>
	<dc:creator>David Turner</dc:creator>
	
	<content:encoded><![CDATA[<h1><a href="http://uni.davidturner.name/533/05.1.-Facebook-Query-Language/" title="Facebook Query Language">Facebook Query Language</a></h1>

<p>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 <em>works</em> and displays that most precious of Facebook related stuff... <strong>data</strong>. 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.</p>

<!--[More]-->

<h2>Starting Point - PHP</h2>

<p>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:</p>

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

<p>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 <em>very</em> 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.</p>

<h2>Facebook Query Language</h2>

<p>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 <em>really</em> need for the starting point I have for my app. These pieces of data are:</p>

<ul>
<li>List of logged in user's friends

<ul>
<li>ID</li>
<li>Name</li>
</ul></li>
<li>List of friends of logged in user's friends</li>
</ul>

<p>To start, we need to create and register our app with Facebook. If you haven't already done so, you really should <a href="http://developers.facebook.com/">head on over</a> and get yourself set up. Doing so will give you the information you need to even be able to <em>try</em> and make any Facebook magic happen. All set? Sweet!</p>

<p>Facebook offered me an example I could use to help get started with things (available <a href="http://www.facebook.com/developers/apps.php">here</a> 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.</p>

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

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

<p>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 <em>little</em> but more useable we can, once again, turn to PHP and a <code>foreach</code> loop:</p>

<pre class="code">
    <code class="php">      
      //test
      foreach($fqlResult as $result){   
        echo $result['uid2']."&lt;br /&gt;";
      }
    </code>
  </pre>

<p>This will loop through all the pieces of the array and echo out the <code>uid2</code> 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.</p>

<p>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.</p>

<h2>Making Life Easier (Update)</h2>

<p>A certain <a href="http://twitter.com/kylegawley/status/28815606568">Kyle Gawley</a> mentioned on twitter another way to list user id <strong>and</strong> 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:</p>

<pre class="code">
    <code class="php">
      $friends = $facebook->api('/me/friends');
    </code>
  </pre>

<p>Because the logged in user is always the person viewing the page, you can run a <em>much</em> 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 <code>print_r($friends);</code> you would see a rather lengthy list of user ids and names wrapped up in all sorts of magical arrays.</p>

<p>So now we're getting data... <em>more</em> 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 <code>foreach</code> loop:</p>

<pre class="code">
    <code class="php">
      foreach ($friends['data'] as $friend) {
        echo $friend['name'].' '.$friend['id']."&lt;br /&gt;";
      }
    </code>
  </pre>

<p>This time it's a little bit different, as the returned information is stored inside an array item labelled <code>data</code>, stored inside the variable <code></code>. We get this information passed into the <code>foreach</code> loop by calling it as <code>$friends['data']</code> 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 <a href="http://twitter.com/kylegawley/">twitterwebs</a>.</p>
]]></content:encoded>
</item></channel>
</rss>