Skip to main content

How To: Create Facebook Events Using Graph API


Creating Facebook Events using the Graph API is fairly simple. And here’s a small code to get you started:

The basic approach


<?php$app_id = "APP_ID";$app_secret = "APP_SECRET";$my_url = "REDIRECT_URL";
$code = $_REQUEST["code"];
if(empty($code)) {    $auth_url = "http://www.facebook.com/dialog/oauth?client_id="    . $app_id . "&redirect_uri=" . urlencode($my_url)    . "&scope=create_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);
$event_url = "https://graph.facebook.com/me/events?" . $access_token;?><!doctype html><html><head><title>Create An Event</title><style>label {float: left; width: 100px;}input[type=text],textarea {width: 210px;}</style></head><body><form enctype="multipart/form-data" action="<?php echo $event_url; ?>" method="post">    <p><label for="name">Event Name</label><input type="text" name="name" value="" /></p>    <p><label for="description">Event Description</label><textarea name="description"></textarea></p>    <p><label for="location">Location</label><input type="text" name="location" value="" /></p>    <p><label for="">Start Time</label><input type="text" name="start_time" value="<?php echo date('Y-m-d H:i:s'); ?>" /></p>    <p><label for="end_time">End Time</label><input type="text" name="end_time" value="<?php echo date('Y-m-d H:i:s', mktime(0, 0, 0, date("m")  , date("d")+1, date("Y"))); ?>" /></p>    <p><label for="picture">Event Picture</label><input type="file" name="picture" /></p>    <p>        <label for="privacy_type">Privacy</label>        <input type="radio" name="privacy_type" value="OPEN" checked='checked'/>Open&nbsp;&nbsp;&nbsp;        <input type="radio" name="privacy_type" value="CLOSED" />Closed&nbsp;&nbsp;&nbsp;        <input type="radio" name="privacy_type" value="SECRET" />Secret&nbsp;&nbsp;&nbsp;    </p>    <p><input type="submit" value="Create Event" /></p></form></body></html>

Our Facebook event form

Explanation

Here we are:
  1. Getting the user access_token
  2. Preparing the Graph API link we are posting to $event_url
  3. And finally, displaying the form with content type “multipart/form-data” for the picture upload

Results

If everything went fine, you should receive the newly created event id:
{
"id": "XXXXXXXXXXXXX"
}
Our event published on Facebook

 


Using the PHP-SDK

1
2
3
4
5
6
7
8
$nextWeek = time() + (7 * 24 * 60 * 60);
$event_param = array(
    "access_token" => "XXXXXXXX",
    "name" => "My Event",
    "start_time" => $nextWeek,
    "location" => "Beirut"
);
$event_id = $facebook->api("/me/events", "POST", $event_param);

 


Using the JS-SDK

1
2
3
FB.api('/me/events','post',{name:"JS-SDK Event",start_time:1272718027,location:"Beirut"},function(resp) {
    alert(resp.id);
});



Important Notes

  • You need the create_event permission
  • Instead of the privacy field use privacy_type; it seems that Facebook is still using the same back-end for publishing the events as the old REST method events.create.
    But don’t try to use this field with other calls or the event FQL table, instead useprivacy
  • Always remember to remove the “timezone” from your start_time and end_timefields to get the expected dates (as recommended by Facebook)
  • While it is not documented, specifying the picture field will create your event with a picture!
  • The “Basic approach” will redirect you to Facebook servers obviously. You may need to post these Data using Ajax or to another end point on your server where you are handling the Graph API call there instead.
    UPDATE: We have written a tutorial on how to handle this case: How To: Create Facebook Events Using Graph API – Advanced

Comments

Popular posts from this blog

75 Surprisingly Creative Facebook Timeline Covers

Now that you have shifted to “Facebook Timeline” to display you profile in a better way, get creative with it. Facebook timeline gives you a chance to turn you profile exclusive and innovative. Putting up a personal picture as cover, will only make you look outdated. You can try some exciting covers to design your very own Facebook profile in distinguished style. We have scrounged through web to pick 75 amazing Facebook Timeline covers for this list. Unique cover pictures can give your timeline a different-from-the-rest look. You can create your own creative covers picking ideas from these cool pictures listed below. If you like this article, you might be interested in some of our other articles on  Facebook Scripts, Best Facebook Apps, Best Facebook Games, and Facebook Tips You Should Check . Ekkapong Techawongthaworn Be a brand ambassador and flaunt your fanaticism for gadgets and shopping on your cover. Ekkapong Techawongthaworn Gabriel Fort A snapshot of an App...

23 Best jQuery Slideshow (Gallery)

JQuery is becoming almost every programmer’s favorite. It gives you freedom of displaying images galleries in most effective way and that too with very little coding. When slideshows have become an integral part of the websites, the importance of jQuery cannot be ignored. Its easy usability has given it’s an edge over Flash. Do you remember the days when we used to see images grouped in tables (tr / td) and once clicked they opened in a new window or pop up! Those days are gone. Now we can use awesome jQuery plugins to showcase images in far more interesting ways. Slideshows provides great user experience. Checking and viewing image galleries have covered a long way in terms of development. Thanks to jQuery. Today we have come up with a power pack of Slideshows provides great user experience. Checking and viewing image galleries have covered a long way in terms of development. Thanks to jQuery. Today we have come up with a power pack of  Best jQuery Slideshow Plugins , which you wo...

GMAP3 – A jQuery Google Maps Plugin For The Developers

GMAP3  is one of the finest Google Maps jQuery Plugin which uses Google Map API version 3 to create maps with the advanced features available in it.  Google themselves has simplified the efforts in adding the maps in any website, but still applying some advance features are tricky sometimes. Unlike the other Google Maps plugin, GMAP3 aims to allows many manipulation of the google map API version 3. Let me take a look on the integration part. As I said, integration Google Maps with GMAP3 is simple and all you need is adding few things to get start with it Step 1:  Add the Google Maps script before closing  </head> <script type = "text/javascript" src = "http://maps.google.com/maps/api/js?sensor=false" ></script> Step 2:  Download the GMAP3 jQuery Plugin and upload it to your server <script type = "text/javascript" src = "gmap3.min.js" ></script> Now in order to embed the Google Maps, do the followi...