Home
Blog
How-To: Use the Graph API to Manage Events

September 16, 2011

How-To: Use the Graph API to Manage Events

By Stephen Doyle

The Graph API already supported creating and deleting events. Now, you can also manage invite lists and check RSVP status for events using the Graph API. All of these new features work from the /EVENT_ID connection, following standard Graph API conventions. The following are the newly introduced APIs for managing events; see the Graph API Event object for full details.



Managing Invite Lists

You can issue the following HTTP requests to view or modify the invite list for an event:

GET /EVENT_ID/invited/USER_ID

Check if USER_ID is invited to the event. This returns an object with name, id, and rsvp_status ('not_replied', 'unsure', 'attending', or 'declined') fields. Requires user_events or friends_events permission for non-public events.

POST /EVENT_ID/invited/USER_ID

Invite a user to an event. Returns true if the request is successful.

POST /EVENT_ID/invited?users=USER_ID1,USER_ID2,USER_ID3

Invite multiple users to an event. Returns true if the request is successful.

DELETE /EVENT_ID/invited/uid

Un-Invite a user from an event. Returns true if the request is successful. The user must be an admin of the event for this call to succeed. Requires rsvp_event permission.

Querying RSVP Status

You can issue the following HTTP requests to query the RSVP status of an event:

GET /EVENT_ID/attending/USER_ID

Check if USER_ID is attending the event (i.e. they RSVP'd yes)

GET /EVENT_ID/maybe/USER_ID

Check if USER_ID RSVP'd 'maybe' for the event

GET /EVENT_ID/declined/USER_ID

Check if USER_ID RSVP'd declined the event (i.e. they RSVP's no)

GET /EVENT_ID/noreply/USER_ID

Check if USER_ID has not yet replied to the event invite

The above methods return an object with name, id, and rsvp_status ('not_replied', 'unsure', 'attending', or 'declined'). If the user is not invited to the event, the API will return an empty data array.

The following example uses the PHP SDK to demonstrate the new methods with several common variations.

<?
  // Remember to copy files from the SDK's src/ directory to a
  // directory in your application on the server, such as php-sdk/
  include_once('php-sdk/facebook.php');

  $config = array(
    'appId' => 'YOUR_APP_ID',
    'secret' => 'YOUR_APP_SECRET',
  );

  // Initialize a Facebook instance from the PHP SDK
  $facebook = new Facebook($config);
  $user_id = $facebook->getUser();

  // Declare the variables we'll use to demonstrate
  // the new event-management APIs

  // We'll create an event in this example.
  // We'll need create_event permission for this.
  $event_id = 0;
  $event_name = "New Event API Test Event";
  $event_start = time();     // We'll just start the event now.
  $event_privacy = "SECRET"; // We'll make it secret so we don't annoy folks.

  // We'll use three users to demostrate the new APIs
  $user_id1 = "USER_ID1";
  $user_id2 = "USER_ID2";
  $user_id3 = "USER_ID3";

  // Convenience method to print simple pre-formatted text.
  function printMsg($msg) {
     echo "<pre>$msg</pre>";
  }
?>
<html>
  <head></head>
  <body>

  <?
    if($user_id) {

      // We have a user ID, so probably a logged in user.
      // If not, we'll get an exception, which we handle below.
      try {

        // Get the user profiles so we can print friendly messages
        $me = $facebook->api('/me', 'GET');
        $user_1 = $facebook->api($user_id1, 'GET');
        $user_2 = $facebook->api($user_id2, 'GET');
        $user_3 = $facebook->api($user_id3, 'GET');

        printMsg('User 1: ' . $user_1['name']);
        printMsg('User 2: ' . $user_2['name']);
        printMsg('User 3: ' . $user_3['name']);

        // Create an event
        $ret_obj = $facebook->api('/me/events', 'POST', array(
                                    'name' => $event_name,
                                    'start_time' => $event_start,
                                    'privacy_type' => $event_privacy
                                 ));

        if(isset($ret_obj['id'])) {
          // Success
          $event_id = $ret_obj['id'];
          printMsg('Event ID: ' . $event_id);
        } else {
          printMsg("Couldn't create event.");
        }
        

        // Invite user 1 to the event
        printMsg('Inviting ' . $user_1['name']);
        $ret_val = $facebook->api($event_id . "/invited/" . $user_id1,
                     'POST');

        if($ret_val) {
          // Success
          printMsg($user_1['name'] . ' successfully invited.');
        } else {
          printMsg("Couldn't invite " . $user_1['name']);
        }
        
        // Check if user 1 is invited to the event
        $ret_val = $facebook->api($event_id . '/invited/' . $user_id1, 'GET');
        
        if($ret_val) {
          printMsg($user_1['name'] . ' is invited (checked).');
        } else {
          printMsg($user_1['name'] . ' is not invited');
        }
        
        // User 1 should be no reply, so we check now.
        // Check in the same way for /attending, /maybe, /declined
        $ret_val = $facebook->api($event_id . '/noreply/' . $user_id1, 'GET');
        
        if($ret_val) {
          printMsg($user_1['name'] . ' has not replied.');
        } else {
          printMsg('Error: ' . $user_1['name'] . ' has replied');
        }

        // Check if user 2 is invited to the event (shouldn't be, yet)
        $ret_obj = $facebook->api($event_id . '/invited/' . $user_id2, 'GET');
          
        // If the user is not invited, we'll get an empty data[] array 
        if(count($ret_obj['data']) > 0) {
          printMsg('Error: ' . $user_2['name'] . ' invited.');
        } else {
          printMsg($user_2['name'] . ' is not invited');
        }   

        // Invite users 2 & 3
        printMsg('Inviting ' . $user_2['name'] . ' and ' . $user_3['name']);
        $ret_val = $facebook->api(
                     $event_id . '/invited?user=' . 
                       $user_id1 . ',' . $user_id2,
                     'POST');

        if($ret_val) {
          printMsg($user_2['name'] . ' and ' . $user_3['name'] . ' invited');
        } else {
          printMsg("Couldn't invite " . $user_2['name'] .
                     ' and ' . $user_3['name']);
        }   

        // Now User 2 should be invited.
        $ret_obj = $facebook->api($event_id . '/invited/' . $user_id2, 'GET');

        // If the user is not invited, we'll get an empty data[] array 
        if(count($ret_obj['data']) > 0) {
          printMsg($user_2['name'] . ' invited (checked).');
        } else {
          printMsg('Error: ' . $user_2['name'] . ' is not invited');
        }   

        // Un-invite user 2
        printMsg('Un-inviting ' . $user_2['name']);
        $ret_val = $facebook->api($event_id . '/invited/' . $user_id2,
                     'DELETE');

        if($ret_val) {
          printMsg($user_2['name'] . ' un-invited.');
        } else {
          printMsg("Couldn't un-invite " . $user_2['name'] );
        }   

        // Creator of the event ("me") should be attending by default
        printMsg('Checking if creator of event (' . 
                  $me['name'] . ') is attending');
        $ret_obj = $facebook->api($event_id . '/attending/' . $me['id'],
                                       'GET');

       // If attending, the data[] array will have our ID in it.
       if(isset($ret_obj['data'][0])) {
         printMsg($ret_obj['data'][0]['name'] . ' is attending');
       } else {
         printMsg('Error: Creator of event is not attending ?!');
       }   

        // Delete the event
        printMsg('Deleting the event.');
        $ret_val = $facebook->api($event_id, 'DELETE');

        if($ret_val) {
          printMsg('Event deleted');
        } else {
          printMsg("Error: Couldn't delete event");
        }   

      } catch(FacebookApiException $e) {
        // If the user is logged out, you can have a 
        // user ID even though the access token is invalid.
        // In this case, we'll get an exception, so we'll
        // just ask the user to login again here.
        $login_url = $facebook->getLoginUrl( array(
                       'scope' => 'create_event, rsvp_event'
                       )); 
        echo 'Please <a href="' . $login_url . '">login.</a>';
        error_log($e->getType());
        error_log($e->getMessage());
      }   
    } else {

      // No user, so print a link for the user to login
      $login_url = $facebook->getLoginUrl( array(
                      'scope' => 'create_event,rsvp_event'
                      )); 
      echo 'Please <a href="' . $login_url . '">login.</a>';
    }   
  ?>  
  </body>
</html>

TAGS

Get our newsletter

Sign up for monthly updates from Meta for Developers.

Sign up