Creating an Event using the Events.Create method in the Facebook API

Published by Utkarsh Patel on 05/02/09 16:31:06
Last edited on 27/04/09 11:23:51

The new Facebook API adds additional method calls that give Facebook Applications additional functionality. One such functionality is the ability to create events using the new events.create method. Although this functionality has been added to the API the supplied php5 library file does not include a function to call this method nor does the facebook wiki page on events.create make it clear how exactly to use this method. This article will show you exactly how to use events.create as there are some special requirements for this method.

Add events_create function to facebookapi_php5_restlib.php

public function &events_create($event_info) {
    return $this->call_method('facebook.events.create',
        array(
  'event_info' => json_encode($event_info),
  'format' => 'XML'));
} 

It would be a good idea to place this function with the other evernt related functions already provided in the file. This function takes in an array of information, json encodes it, using the built in php function json_encode($array), and passes it in as a parameter to facebook.events.create method. The structure of $event_info is defined below.

Required parameters of $event_info
name - string
category - int
subcategory - int
location - string
city - string - Must be a valid city name or else the method will throw an error
start_time - utc time - look at example below on how to convert a time to UTC
end_time - utc time 
Optional parameters of $event_info
street - string
phone - string
email - string
page_id - int
host - string
desc - string
privacy_type - int
tagline - string 
Using events_create
include_once 'client/facebook.php';
include_once 'config.php';

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

//Setup array with paramets you would like to pass into events.create
$event_info = array();
$event_info['name'] = 'Party';
$event_info['category'] = 1;
$event_info['subcategory'] = 1;
$event_info['host'] = 'You';
$event_info['location'] = 'Your House';
$event_info['city'] = 'Toronto'; //Must be a valid city name
$event_info['start_time'] = gmmktime(22,0,0,9,3,2008); //Converts time to UTC
$event_info['end_time'] = gmmktime(5,0,0,9,5,2008); //COnverts time to UTC

//Call events_create
//Display events id on event creation
//Display error message with error code on error
try
{
 $event_id = $fapi->events_create($event_info);
 echo 'Event Created! Event Id is: '.$event_id;
}
catch(Exception $e)
{
 echo 'Error message: '.$e->getMessage().' Error code:'.$e->getCode();
}
And there you have it, a simple yet effective way to using the new events.create method call in the Facebook API.

About the Author

Utkarsh Patel is one of our newest members, and he could not have come at a better time. He has helped bring back stability and control to Techlicity Ventures during our rapid growth. If he isn't marvelling at a recently discovered Oracle feature or technique he is bothering the rest of the team on why we should move everything over to Oracle databases. Utkarsh complements the team well with his determination and sound research.

Bookmark and Share
Blog Widget by LinkWithin
blog comments powered by Disqus

Valid XHTML 1.0!