Facebook Programming: API Events


Facebook Application Programming Step by step tutorial - part 5. On Facebook, users can keep a list of activities. Thus, the user can also use Facebook to optimize the planning himself. Facility is called Events. Fun anymore, we can invite friends to join the event we have made. Example event page views you can see in Figure 3.4.

In addition to showing users their own activity, Facebook also displays the recommended activities to him.

Facebook allows us to access the event data. That means you can create event-based applications. That's why there are APIs for events. There is some method to the API Events. Here is a list of API Events:

Method Description
events.cancel Cancel an event / activity. Action should be carried out by admin for an event.
events.create Creating an event
events.edit Editing an existing event
events.get Menampilkan semua event berdasarkan filter
events.getMembers Showing Membership an event
events.invite Inviting the user to an event
events.rsvp Setting the arrival options

facebook3.4
Figure 3.4 Display event page

Showing Events

Formula to show the event as follows:

$facebook->api_client->events_get($uid, $eids, $start_time, $end_time, $rsvp_status);
  • $uid is the user ID you want to display eventnya.
  • $eids is ID event. Like as a user ID, as well as with the event. Each event has a ID which is usually called $eid.
  • $start_time and $end_time is a parameter to filter out events that will be displayed. $start_time means the beginning of time, while $end_time is the final time. Standard time used is UTC date/time value. Do not worry, PHP has a function called time() to convert time in accordance with this method request.
  • $rsvp_status is the status of arrival. The value of this status is attending (sure to come), unsure, declined, or not_replied.

Well, let's try this method first. Now run the following programs:

<?php
// Get these from http://developers.facebook.com
$api_key = '2c73ae85291aaaaaaaaaaaaaaaaaaaaa';
$secret  = '3c041a7cbd7aaaaaaaaaaaaaaaaaaaaa';

include_once './facebook-platform/php/facebook.php';

$facebook = new Facebook($api_key, $secret);
$user = $facebook->require_login();

?>
<h1>Yummie Tester</h1>
Hello <fb:name uid='<?php echo $user; ?>' useyou='false' possessive='true' />! <br>
Your id : <?php echo $user; ?>.<br>

<h2>Event<h2>
<?
$events = $facebook->api_client->events_get(null, null, null, null, null);

echo "<h3>Return array from Facebook</h3>";
print_r($events);
?>

The result you can see in Figure 3.5. Now we analyze the code above. We write a line of code to display the event as follows:

$events = $facebook->api_client->events_get(null, null, null, null, null);

Because we are filled with null parameters, meaning we do not do any filtering. We will display all events. However, although the $eid filled null, it does not mean displaying events for all users. Automatically displayed only event you have (or users who access these pages).

facebook3.5
Figure 3.5 Showing event in array format

If we look, the data presented in array consists of:

  • eid is the id of the event.
  • name is the name or title of the event.
  • tagline is an interesting short sentences to attract users to visiting the page event.
  • nid is ID maker event.
  • pic is the URL of the image that appear in the event. The maximum width of 100px and height 300px.
  • pic_big is URL a large image. The maximum width of 200px and height 600px.
  • pic_small is a small image. Maximum width 50px and height 150px.
  • host is the name of the event creator.
  • description is a brief description or explanation of the event.
  • event_type is the type of event. To fill it, you just choose a list of types that have been provided by Facebook.
  • event_subtype are details of the event type. To fill it, you just choose a list of subtype which has been provided by Facebook.
  • start_time is the start time of the event.
  • end_time the expiration of the event.
  • creator is the ID of the maker.
  • update_time is the time to register or to update the event on Facebook.
  • location is the place or location of the event took place.
  • venue is the details of where the event took place. In the data there are street venue, city, state, and country.

These results, if written in array format will look like the following:

Array ( [0] => Array 
            ( [eid] => 191257662149 
               [name] => Obrolan Senja 
               [tagline] => Ayo berpartisipasi
               [nid] => 0 
               [pic] => http://profile.ak.fbcdn.net/object2/11/124/s19277.jpg 
               [pic_big] => http://profile.ak.fbcdn.net/object2/1/124/n19.jpg 
               [pic_small] => http://profile.ak.fbcdn.net/object2/18/14/t162.jpg 
               [host] => Faiz Ah 
               [description] => Hi..! APA KABAR FIKSI FANTASI? 
               [event_type] => 
               [event_subtype] => 
               [start_time] => 1257463800 
               [end_time] => 1257474600 
               [creator] => 1524500000 
               [update_time] => 1257130015 
               [location] => Rangkas Club, Jl. Sand 101, Yogyakarta 
              [venue] => Array ( [street] => 
                                                 [city] => 
                                                 [state] => 
                                                 [country] => ) 
               [privacy] => OPEN 
               [hide_guest_list] => 0 
               [show_in_search] => 0 
)

Here we will modify the code to be nice appearance:

<?php
// Get these from http://developers.facebook.com
$api_key = '2c73ae85291aaaaaaaaaaaaaaaaaaaaa';
$secret  = '3c041a7cbd7aaaaaaaaaaaaaaaaaaaaa';

include_once './facebook-platform/php/facebook.php';

$facebook = new Facebook($api_key, $secret);
$user = $facebook->require_login();

?>
<h1>Yummie Tester</h1>
Hello <fb:name uid='<?php echo $user; ?>' useyou='false' possessive='true' />! <br>
Your id : <?php echo $user; ?>.<br>

<h2>Event<h2>
<?
$events = $facebook->api_client->events_get(null, null, null, null, null);

//echo "<h3>Return array from Facebook</h3>";
//print_r($events);
?>
<table>
<?
foreach($events as $event){
?>
	<tr>
      <td colspan="2"><?=date("l dS F Y",$event['start_time'])?></td>
    </tr>
	<tr>
      <td><img src="<?=$event['pic_small']?>"></td>
      <td valign="top">
	  	<?=$event['name']?>
      	<br>
        <?=$event['tagline']?>
      	<br><br>
        Type: <?=$event['event_type']?> - <?=$event['event_subtype']?><br>        
        Where: <?=$event['location']?><br>        
        When: <?=date("dS F Y h:i",$event['start_time'])?> - <?=date("dS F Y h:i",$event['end_time'])?><br>                
      </td>
    </tr>
    <tr>
      <td> </td>
    </tr>
<?	
}
?>
</table>

The result:

facebook3.6
Figure 3.6 Event Display

Showing Member from Event

To display the member of an event, you can use the following formula:

$facebook->api_client->events_getMembers($eid);

Following simple example:

<?php
// Get these from http://developers.facebook.com
$api_key = '2c73ae85291aaaaaaaaaaaaaaaaaaaaa';
$secret  = '3c041a7cbd7aaaaaaaaaaaaaaaaaaaaa';

include_once './facebook-platform/php/facebook.php';

$facebook = new Facebook($api_key, $secret);
$user = $facebook->require_login();

?>
<h1>Yummie Tester</h1>
Hello <fb:name uid='<?php echo $user; ?>' useyou='false' possessive='true' />! <br>
Your id : <?php echo $user; ?>.<br>

<h2>Event<h2>
<?
$events = $facebook->api_client->events_getMembers(141015406075);

echo "<h3>Return array from Facebook</h3>";
print_r($events);
?>

The output like this:

Array ( [attending] => Array 
                      ( [0] => 821214608 
                         [1] => 1535126550 
                         [2] => 1557821758 
                         [3] => 1231450659 
                         [4] => 857574311 
                         [5] => 1649292533 
                         [6] => 1613413783 
                         [7] => 100000334262668 
                         [8] => 1289626293 
                         [9] => 1211923468 
                         [10] => 770277356
                     )
      [unsure] => Array
                    ( [0] => 1506487084 
                       [1] => 1398533601 
                       [2] => 1481580815 
                       [3] => 1640106742 
                       [4] => 1138298482 
                       [5] => 1584435287
                    )
      [declined] => Array
                    ( [0] => 1094043520 
                       [1] => 1357102927 
                       [2] => 100000177951664 
                       [3] => 1538477908 
                       [4] => 1245755031 
                       [5] => 718354339
                    )
        [not_replied] =>
)

To show the name of those ID, you can modify the code like this:

<?php
// Get these from http://developers.facebook.com
$api_key = '2c73ae85291aaaaaaaaaaaaaaaaaaaaa';
$secret  = '3c041a7cbd7aaaaaaaaaaaaaaaaaaaaa';

include_once './facebook-platform/php/facebook.php';

$facebook = new Facebook($api_key, $secret);
$user = $facebook->require_login();

?>
<h1>Yummie Tester</h1>
Hello <fb:name uid='<?php echo $user; ?>' useyou='false' possessive='true' />! <br>
Your id : <?php echo $user; ?>.<br>

<h2>Event<h2>
<?
$events = $facebook->api_client->events_getMembers(141015406075);

echo "<h3>Attending</h3>";

echo "<ol>";

if($events['attending'][0]){
  foreach($events['attending'] as $member){
	echo "<li><fb:name uid=\"$member\" useyou=\"false\"></li>";  
  }	
}else{
  echo "None";	
}

echo "</ol>";


echo "<h3>Unsure</h3>";

echo "<ol>";

if($events['unsure'][0]){
  foreach($events['unsure'] as $member){
	echo "<li><fb:name uid=\"$member\" useyou=\"false\"></li>";  
  }	
}else{
  echo "None";	
}

echo "</ol>";

echo "<h3>Declined</h3>";

echo "<ol>";

if($events['declined'][0]){
  foreach($events['declined'] as $member){
	echo "<li><fb:name uid=\"$member\" useyou=\"false\"></li>";  
  }	
}else{
  echo "None";	
}

echo "</ol>";

echo "<h3>Not Replied</h3>";

echo "<ol>";

if($events['not_replied'][0]){
  foreach($events['not_replied'] as $member){
	echo "<li><fb:name uid=\"$member\" useyou=\"false\"></li>";  
  }	
}else{
  echo "None";	
}

echo "</ol>";
?>

The output like this:

facebook3.7
Figure 3.7 List of event's member and their status

To be continued... ^^!

Previous: Facebook Programming: API Friends
Next Facebook Programming: API Groups




Bookmark and Share Tag: facebook, api Category: PHP API Post : November 10th 2009 Read: 77,077

advertisements


blog comments powered by Disqus