Facebook API – User Events

This a follow-up of my recent article about reading the guest list of a Facebook Event created through Facebook Pages. If you want to get the Events (and the attendees) of a User, it´s a bit more work. You need to authorize the User with the permission “user_events”, and you will still just get the Events the user is attending too with the “/events” connection. I spare you the details of creating an app, this is just the authorization code for the PHP SDK:

$facebook->getLoginUrl(array('scope' => 'user_events'))

Let´s get the events

Here´s how to get the Events of a Facebook Page:

$events = $facebook->api('/' . $_GET['pageid'] . '/events');
$events = $events['data'];
foreach ($events as $event)
{
	...
}

Getting the Events the User is attending to is almost the same:

$events = $facebook->api('/me/events');
$events = $events['data'];

Remember, this will not get you the Events the user created. For that, you will have to dig deeper and use FQL (Facebook Query Language):

$fql = 'SELECT eid, name, start_time FROM event
	WHERE creator = ' . $userid . ' AND eid IN
	(SELECT eid FROM event_member WHERE uid = ' . $userid . ')
	ORDER BY start_time desc';
$param  =   array(
	'method' => 'fql.query',
	'query' => $fql,
	'callback' => ''
);
$events = $facebook->api($param);
foreach ($events as $event)
{
	...
}

The Events will be sorted descending by their starting date, newer Events will show up first:

$timestamp = time();
$fql = 'SELECT eid, name, start_time FROM event
	WHERE creator = ' . $userid . ' AND start_time > ' .
	$timestamp . ' AND eid IN (SELECT eid FROM event_member
	WHERE uid = ' . $userid . ')
	ORDER BY start_time desc';

Links

  1. Facebook Query Language (FQL)
  2. Facebook Graph API
  3. Event Attending App

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.