Monday, September 26, 2011

HowTo: Facebook Event Stream

I recently refactored the code for a few parts of the CSSS website. One of the sections needing updating was our Facebook Groups event stream, as it had seemed to have stopped working.

This module allowed us to automatically display the events that were created on our Facebook page, without any further work. This means that any admin of the Facebook Group had the capabilities to publish events to our website, without having access to the source code or the server itself. This is a good thing when you have first-years on the executive.

Currently the only way to set up this feature requires a Facebook work around, as there is no direct way to export the event feed from a group. There is however a feature request for this issue, however it has already been in the queue for a couple years now, an I don't expect to see a resolution any time soon.

To make a workaround you will first need to create a new profile on Facebook, and lock down the privacy settings for this new user. Next you need to subscribe this user to your Facebook Group. This way when a new event is created in the group, they will automatically be invited. You can now utilize the event feed for your new user as the event feed for your groups events. This method doesn't exactly follow Facebook's terms of service, however until they provide a way for groups to export their event feed, this will be the only way to do so.

The code required to get this site module working is available on my GitHub. Although this code isn't super efficient or elegant, it does get the job done.

This segment below will call the Event Feed Module and place it on your site.

   1:  <?php @include("includes/events.php"); ?>

Next I will go through the events.php code to show the components that can be modified to suit your own needs.

To start off your event feed you will need to provide it with the Facebook event feed from the profile you created earlier.

   1:  <?php
   2:  $site = 'http://www.facebook.com/ical/u.php?uid=YOUR-ID-HERE.ics';

The next function iCalDecoder will take the event stream and form an array out of each event and its specific details. This way we can easily iterate through the events and only display the useful information on our site.

Before we can do this however the events need to have their dates and times formated for the local time zone. We do this with the formatTime function.

Now that our events are easily addressable and reflect the correct times we can just cycle through each of them to create our feed.

   1:  $maxDisplay = 2; // How many events should be displayed on the feed
   2:  $arrayCount = count($icalarray);
   3:  // Display's $maxDisplay number of events in the site feed
   4:  for($i = $arrayCount-1; $i >= 0; $i--){
   5:      if($i == $arrayCount - $maxDisplay - 1){
   6:      echo '<div id="hidden_events" style="display: none;">';
   7:  }
   8:   
   9:  $time = formatTime($icalarray[$i]['DTSTART']);
  10:  echo '<h2><a href="'.$icalarray[$i]['URL'].'">'.$icalarray[$i]['SUMMARY'].'</a></h2>';
  11:  echo '<small>Date: '.$time['MONTH'].' '.$time['DAY'].', '.$time['YEAR'].' at '.$time['HOUR'].':'.$time['MIN'].' '.$time['TOD'].'<br />';
  12:  echo 'Location: '.$icalarray[$i]['LOCATION'].'</small>';

This final section of code will wrap up our events.php, and were done.

You can get the entire events.php file used.