Implementing Facebook Event API With fql.multiquery and With Graph Api

Facebook is our part of lifestyle and people are using this to create event so that friends can easily RSVP. Facebook Event is getting quite popular. Many Facebook application and website are using Facebook event api to show users events on their site and apps. Recently I added Facebook event functionality in http://apps.facebook.com/calendarised/. In this app, all events are imported to the user calender entries. There is iphone version of this app. Both the facebook app and the iphone app is synchronized. User can create friend’s birthday as events and you can create a events. There lots more feature give it it a try http://itunes.apple.com/us/app/calendarised/id445902228 ;). There is also a cron job running if the user wants to import Facebook events to add automatically. In this post i will show how you can implement Facebook event api. In this post i will also show two example of fql and fql.multiquery. So you will get a idea about fql two also.

Before we start we have to change a configuration. If you don’t set this then the user will get the event time of their time zone not the actual time of the event. I recent wasted one night trying to figure out why iwas not getting correct time. Also you have ask user_events permission and friends_events is you want get friends events.

Set it like this in app dev in the https://developers.facebook.com/apps

The authentication code is below

$facebook = new Facebook(array(
  'appId'  => FACEBOOK_APP_ID,
  'secret' => FACEBOOK_SECRET_KEY
));
 
$user = $facebook->getUser();
 
$loginUrl   = $facebook->getLoginUrl(
            array(
                'scope'         => 'user_events,friends_events'
            )
    );
 
if ($user) {
  try {
        // Proceed knowing you have a logged in user who's authenticated.
       $user_profile = $facebook->api('/me');;
  } catch (FacebookApiException $e) {
    $user = null;
  }
}  
 
if (!$user) {
    echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
}

Now there are 4 types of event you can get from a user events.

  • Attending : Events that the user clicked “Attending” on event page.
  • Maybe: Events that the user clicked “Maybe” on event page.
  • Declined: Events that the user clicked “No” on event page.
  • May be: Events that the user has not clicked any button on the event page.

Now I am going to use fql.multiquery because it better to use it then calling api 4 times for 4 types of event. Basically i prefer fql for performance and with fql.multiquery i can do a lot more with api cal.

	$multiQuery = '{
			 "attending":"SELECT name, start_time, end_time, location, description FROM event WHERE eid IN ( SELECT eid FROM event_member WHERE uid = '.$user.' AND rsvp_status=\'attending\')",
			 "maybe":"SELECT name, start_time, end_time, location, description FROM event WHERE eid IN ( SELECT eid FROM event_member WHERE uid = '.$user.' AND rsvp_status=\'unsure\')",
			 "declined":"SELECT name, start_time, end_time, location, description FROM event WHERE eid IN ( SELECT eid FROM event_member WHERE uid = '.$user.' AND rsvp_status=\'declined\')",
			 "not_replied":"SELECT name, start_time, end_time, location, description FROM event WHERE eid IN ( SELECT eid FROM event_member WHERE uid = '.$user.' AND rsvp_status=\'not_replied\')",
			 }';
	
	$param = array(       
		 'method' => 'fql.multiquery',       
		 'queries' => $multiQuery,       
		 'callback' => '');
	try{       
		$events = $facebook->api($param);
	}catch(Exception $e){echo $e->getMessage();}

If you want all the events then you just run this fql

	$event_details = $facebook->api(array('method' => 'fql.query','query' => 
'SELECT name, start_time, end_time, location, description FROM event WHERE eid IN ( SELECT eid FROM event_member WHERE uid = '.$user.')'   ));	

Now i will show you how you can do this with graph api. Now i put limit 200 for paging. By default it returns 20 per page. But rather looping and calling api – i put 200. My guess is 200 is pretty big for user events.

$events_attending=$facebook->api("/$user/events/attending?limit=200");
$events_maybe=$facebook->api("/$user/events/maybe?limit=200");
$events_declined=$facebook->api("/$user/events/declined?limit=200");
$events_not_replied=$facebook->api("/$user/events/not_replied?limit=200");

To get all the events together

$events=$facebook->api("/$user/events?limit=1000");

Pretty simple right?

About Zakir Hyder

This entry was written by .

16. September 2011 by Zakir Hyder
Categories: Facebook, Graph Api, PHP, Social Networking Application Development | Tags: , , , , , , , , , | Comments

Comments

  1. [...] Implementing Facebook Event API With fql.multiquery and With Graph Api September 16, 2011 10:21 PM Facebook is our part of lifestyle and people are using this to create event so that friends can easily RSVP. Facebook Event is getting quite popular. Many Facebook application and website are using Facebook event api to show users events on their site and apps. Recently I added Facebook event functionality in http://apps.facebook.com/calendarised/. In this [...] [...]