Events.create

From Facebook Developer Wiki

Jump to: navigation, search

Contents

Description

Creates an event on behalf of the user if the application has an active session key for that user; otherwise it creates an event on behalf of the application. Applications can create events for a user if the user grants the application the create_event extended permission.

If you are creating an event on behalf of a user, then your application is an admin for the event, while the user is the creator.

You can upload an image and associate it with the event by forming the request as a MIME multi-part message. See photos.upload for details on the message format to use and the supported image types. You can replace or delete images in an event using events.edit.

This method does not require a session key. However if you call this method without an active user session, then your application is both the creator and admin for the event.

Parameters

RequiredNameTypeDescription
required api_key string The application key associated with the calling application. If you specify the API key in your client, you don't need to pass it with every call.
call_id float The request's sequence number. Each successive call for any session must use a sequence number greater than the last. We suggest using the current time in milliseconds, such as PHP's microtime(true) function. If you specify the call ID in your client, you don't need to pass it with every call.
sig string An MD5 hash of the current request and your secret key, as described in the How Facebook Authenticates Your Application. Facebook computes the signature for you automatically.
v string This must be set to 1.0 to use this version of the API. If you specify the version in your client, you don't need to pass it with every call.
event_info object The event information, passed as a JSON-encoded object literal. See the Notes below for information on what parameters to include in the object.
optional session_key string The session key of the logged in user. The session key is automatically included by our PHP client.
format string The desired response format, which can be either XML or JSON. (Default value is XML.)
callback string Name of a function to call. This is primarily to enable cross-domain JavaScript requests using the <script> tag, also known as JSONP, and works with both the XML and JSON formats. The function will be called with the response passed as the parameter.
[no name] data The raw image data of the picture to associate with the event.

Example Requests

Example Code in PHP

The $_POST[''] - Array have to be filed with the necessary variables so you can design a formular and send it via the method "POST" to a file that contains the following PHP-Code (replace the XXXX... with your personal settings): <?php //declaration require_once 'facebook.php'; $appapikey = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'; $appsecret = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'; $facebook = new Facebook($appapikey, $appsecret); $user_id = $facebook->require_login(); if(!$facebook->api_client->users_hasAppPermission('create_event')){ echo'<script type="text/javascript">window.open("http://www.facebook.com/authorize.php?api_key='.$appapikey.'&v=1.0&ext_perm=create_event", "Permission");</script>'; echo'<meta http-equiv="refresh" content="0; URL=javascript:history.back();">'; exit; } $_POST['start_time']=mktime($_POST['start_time_hour'],$_POST['start_time_min'],"00",$_POST['start_time_month'],$_POST['start_time_day'],$_POST['start_time_year']); $_POST['end_time']=mktime($_POST['end_time_hour'],$_POST['end_time_min'],"00",$_POST['end_time_month'],$_POST['end_time_day'],$_POST['end_time_year']); unset($_POST['start_time_hour']); unset($_POST['start_time_min']); unset($_POST['start_time_month']); unset($_POST['start_time_day']); unset($_POST['start_time_hour']); unset($_POST['end_time_hour']); unset($_POST['end_time_min']); unset($_POST['end_time_month']); unset($_POST['end_time_day']); unset($_POST['end_time_hour']); try{ $event_id=$facebook->api_client->events_create(json_encode($_POST)); echo'<meta http-equiv="refresh" content="0; URL=">'; }catch(Exception $e){ echo 'Error message: '.$e->getMessage().' Error code:'.$e->getCode(); } ?> OBS: It´s important to have in mind that this function appears only to work with utf8 characters. So, before you send the array to the function, you have to encode it properly. Here´s how it worked with us at http://keroir.com: <?PHP $event_fb = array_map(utf8_encode, $event_fb); $event_id = $facebook->api_client->events_create(json_encode($event_fb)); ?> Note that ''$event_fb'' is the array with the data to be passed to Facebook events_create. Hope you have luck with it!

Example Return XML

<?xml version="1.0" encoding="UTF-8"?> <events_create_response xmlns="http://api.facebook.com/1.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://api.facebook.com/1.0/ http://api.facebook.com/1.0/facebook.xsd">34444349712 </events_create_response>

Error Codes

CodeDescription
1 An unknown error occurred. Please resubmit the request.
2 The service is not available at this time.
4 The application has reached the maximum number of requests allowed. More requests are allowed once the time window has completed.
5 The request came from a remote address not allowed by this application.
100 One of the parameters specified was missing or invalid (e.g. missing location, etc.)
101 The API key submitted is not associated with any known application.
102 The session key was improperly submitted or has reached its timeout. Direct the user to log in again to obtain another key.
103 The submitted call_id was not greater than the previous call_id for this session.
104 Incorrect signature.

Notes

You must pass the following parameters in the event_info JSON object literal. The parameters are explained in Event (FQL).

Note: The start_time and end_time are the times that were input by the event creator, converted to UTC after assuming that they were in Pacific time (Daylight Savings or Standard, depending on the date of the event), then converted into Unix epoch time. Basically this means for some reason facebook does not want to get epoch timestamps here, but rather something like epoch timestamp minus 7 or 8 hours, depeding on the date. have fun! (Note: I created a bug report for this since it is incredibly complex to require all devs to determine PST vs. PDT. This call should accept UTC time. Please vote for that bug!)

Optionally, you can pass the following parameters in the event_info object:

  • street
  • city
  • phone
  • email
  • page_id
  • description
  • privacy_type
  • tagline

Note: The page_id argument can be used to (see e.g. browser line and find out the id or eid (11 numbers)):

  • generate events for groups
  • generate events for pages

Note: The privacy_type argument can be one of:

  • OPEN, for an event open and visible to everyone.
  • CLOSED, for an event that is visible to everyone but requires an invitation.
  • SECRET, for an event that is invisible to those who have not been invited.

So, a sample event_info object would look like this:

{"name":"name","category":"1","subcategory":"1","host":"host","location":"location","city":"Palo Alto, CA","start_time":1215929160,"end_time":1215929160}

Using the Session Secret

You can call this method using a session secret, and not the application secret (for example, for a Facebook Connect site or desktop application).

See Also

reference