Warning: Creating default object from empty value in /home/ibrfao/masteringapi.com/blog/wp-content/themes/platform/includes/class.layout.php on line 164

Warning: Creating default object from empty value in /home/ibrfao/masteringapi.com/blog/wp-content/themes/platform/includes/class.layout.php on line 167

Warning: Creating default object from empty value in /home/ibrfao/masteringapi.com/blog/wp-content/themes/platform/includes/class.layout.php on line 170

Warning: Creating default object from empty value in /home/ibrfao/masteringapi.com/blog/wp-content/themes/platform/includes/class.layout.php on line 173

Warning: Creating default object from empty value in /home/ibrfao/masteringapi.com/blog/wp-content/themes/platform/includes/class.layout.php on line 176

Warning: Creating default object from empty value in /home/ibrfao/masteringapi.com/blog/wp-content/themes/platform/includes/class.layout.php on line 178

Warning: Creating default object from empty value in /home/ibrfao/masteringapi.com/blog/wp-content/themes/platform/includes/class.layout.php on line 180

Warning: Creating default object from empty value in /home/ibrfao/masteringapi.com/blog/wp-content/themes/platform/includes/class.layout.php on line 202

Warning: Creating default object from empty value in /home/ibrfao/masteringapi.com/blog/wp-content/themes/platform/includes/class.layout.php on line 206

Warning: Creating default object from empty value in /home/ibrfao/masteringapi.com/blog/wp-content/themes/platform/includes/class.layout.php on line 224

Warning: Creating default object from empty value in /home/ibrfao/masteringapi.com/blog/wp-content/themes/platform/includes/class.layout.php on line 225

Warning: Creating default object from empty value in /home/ibrfao/masteringapi.com/blog/wp-content/themes/platform/includes/class.layout.php on line 227

Warning: Creating default object from empty value in /home/ibrfao/masteringapi.com/blog/wp-content/themes/platform/includes/class.layout.php on line 321

Warning: Creating default object from empty value in /home/ibrfao/masteringapi.com/blog/wp-content/themes/platform/includes/class.layout.php on line 321

Warning: Creating default object from empty value in /home/ibrfao/masteringapi.com/blog/wp-content/themes/platform/includes/class.layout.php on line 321

Warning: Creating default object from empty value in /home/ibrfao/masteringapi.com/blog/wp-content/themes/platform/includes/class.layout.php on line 321

Warning: Creating default object from empty value in /home/ibrfao/masteringapi.com/blog/wp-content/themes/platform/admin/class.options.metapanel.php on line 56

Warning: Creating default object from empty value in /home/ibrfao/masteringapi.com/blog/wp-content/themes/platform/admin/class.options.metapanel.php on line 56

Warning: Creating default object from empty value in /home/ibrfao/masteringapi.com/blog/wp-content/themes/platform/admin/class.options.metapanel.php on line 56

Warning: Creating default object from empty value in /home/ibrfao/masteringapi.com/blog/wp-content/themes/platform/admin/class.options.metapanel.php on line 49
How To: Check Status And RSVP To Facebook Events Using Graph API & FQL | MasteringAPI

After learning how to create Facebook events using the Graph API, it’s time now to learn how to RSVP to Facebook events and how to check the user’s RSVP status to an event.

 

Permissions

First things first, you need the rsvp_event permission to be able to RSVP to events.

Back to top
 

Check the user’s RSVP status

Checking if the user is attending an event or not is quite simple:

$query = "SELECT rsvp_status FROM event_member WHERE eid=$EVENT_ID AND uid=me()";
$fql_url = "https://api.facebook.com/method/fql.query?"
	. "query=" . urlencode($query)
	. "&format=json"
	. "&" . $access_token;
$fql_resp = json_decode(file_get_contents($fql_url));
$rsvp_status = $fql_resp[0]->rsvp_status;

As mentioned in the event_member table documentation this would return attending, unsure, declined or not_replied.

Note
You can always replace me() with the user id and also use an infinite access_token (by requesting the offline_access permission) if the user may not be online.

Back to top
 

RSVP to an event

Issuing a call to the Graph API with the RSVP status is all you need:

curl -d “access_token=XXX”

https://graph.facebook.com/EVENT_ID/{attending|maybe|declined}

And here’s a simple example to get you started:

<?php
$app_id = "APP_ID";
$app_secret = "APP_SECRET";
$my_url = "REDIRECT_URI";
$event_id = "EVENT_ID";
$rsvp_status = "";

$code = $_REQUEST["code"];

if(empty($code)) {
	$auth_url = "http://www.facebook.com/dialog/oauth?client_id="
	. $app_id . "&redirect_uri=" . urlencode($my_url)
	. "&scope=rsvp_event";
	echo("<script>top.location.href='" . $auth_url . "'</script>");
}

$token_url = "https://graph.facebook.com/oauth/access_token?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url)
. "&client_secret=" . $app_secret
. "&code=" . $code;
$access_token = file_get_contents($token_url);

if( isset($_POST['rsvp']) ) {
	// Form submitted, call the Graph API to RSVP to the event
	$event_rsvp = "https://graph.facebook.com/$event_id/{$_POST['rsvp']}?method=post&" . $access_token;
	$rsvped = json_decode(file_get_contents($event_rsvp));
	if($rsvped) {
		$msg = "Your RSVP status is now <strong>{$_POST['rsvp']}</strong>";
		$rsvp_status = $_POST['rsvp'];
	}
}
if( !$rsvp_status ) {
	$query = "SELECT rsvp_status FROM event_member WHERE eid=$event_id AND uid=me()";
	$fql_url = "https://api.facebook.com/method/fql.query?"
		. "query=" . urlencode($query)
		. "&format=json"
		. "&" . $access_token;
	$fql_resp = json_decode(file_get_contents($fql_url));
	$rsvp_status = $fql_resp[0]->rsvp_status;
}
?>
<!doctype html>
<html>
<head>
<title>Create An Event</title>
<style>
label {float: left; width: 100px;}
input[type=text],textarea {width: 210px;}
#msg {border: 1px solid #000; padding: 5px; color: red;}
</style>
</head>
<body>
<?php if( isset($msg) ) { ?>
<p id="msg"><?php echo $msg; ?></p>
<?php } ?>
<form action="" method="post">
	<p>
		<label for="privacy_type">RSVP:</label>
		<input type="radio" name="rsvp" value="attending" <?php if($rsvp_status==="attending") echo "checked='checked'"; ?>/>Attending&nbsp;&nbsp;&nbsp;
		<input type="radio" name="rsvp" value="maybe" <?php if($rsvp_status==="maybe" || $rsvp_status==="unsure") echo "checked='checked'"; ?>/>Maybe&nbsp;&nbsp;&nbsp;
		<input type="radio" name="rsvp" value="declined" <?php if($rsvp_status==="declined") echo "checked='checked'"; ?>/>Not Attending&nbsp;&nbsp;&nbsp;
	</p>
	<p><input type="submit" value="RSVP to this event" /></p>
</form>
</body>
</html>
Back to top
  • Brunoguic

    Hi! You used  the  Old REST API(api.facebook.com) with fql right? But in Facebook documentation indicates to use Graph API(graph.facebook.com). It’s a big doubt about these many access platform.1)Can i do these example only with Graph API?2)Can i use FQL with GraphAPI?thank man, your site is fantastic! =)

    • http://www.masteringapi.com Ibrahim Faour

       The only way to access FQL is using the method mentioned above this is why I explicitly mentioned FQL in the title along with Graph API. If you check the documentation, you’ll notice that this is the way to call FQL. Thanks for passing by!

      • Brunoguic

         Thank you man! Sorry about noob question, but I think this documentation so poor!
        Your site is 1000% more useful and clear!
        =)

        • http://www.masteringapi.com Ibrahim Faour

           You are welcome man!
          Don’t forget to subscribe to our RSS feed and follow me on twitter! ;-)

  • Brunoguic

    Hi Ibrahim!
    I’m confused about when use FQL ou GraphAPI..

    Can i use instead of FQL this??if( !$rsvp_status ) {    $url = “https://graph.facebook.com/me/events/?” . $access_token;    $result = file_get_contents($url);    $decoded = json_decode($result);    if(is_array($decoded) && isset($decoded['id'])) {        foreach($decoded as $indice => $valor){            if($decoded[$indice]->id == $event_id){                $rsvp_status = $decoded[$indice]->rsvp_status;            }        }    }}

    • http://www.masteringapi.com Ibrahim Faour

       Well, you can use this but why would you? :-)
      Still not convinced of using FQL?
      Anyway, I wouldn’t recommend retrieving ALL events just to get the RSVP status of one of them.
      P.S: if you want to go with your approach just keep in mind that there’s paging! so your desired event may not be in the result you are looping!

    • http://www.masteringapi.com Ibrahim Faour

       Well, you can use this but why would you? :-)
      Still not convinced of using FQL?
      Anyway, I wouldn’t recommend retrieving ALL events just to get the RSVP status of one of them.
      P.S: if you want to go with your approach just keep in mind that there’s paging! so your desired event may not be in the result you are looping!

  • Oliver

    is it possible to remove a event rsvp /w php?

    and do u know how i can post to a events wall using my page?

  • Altinnovation

    Hey great work! Would it be possible to show the image icons of 5 people attending that event :)?

  • Avi

    Thanks for this article it is great. Would you know how can I check in a user to a place?

  • Danny

    Could someone please tell me what the “$code = $_REQUEST["code"];” does? I’m trying to modify this piece of code to work with codeigniter

    Could someone tell me what i’m doing wrong here: http://pastebin.com/C2dBHA9X