Following our previous tutorial: How To: Create Facebook Events Using Graph API

And to insure that we cover as much information as we can, we are going to show you today how to use the similar (previous) approach but without redirecting the user to Facebook (using cURL).

 

Our code

For this we have modified our previous code as follows:

<?php
$app_id = "APP_ID";
$app_secret = "APP_SECRET";
$my_url = "REDIRECT_URL"; // mainly this should be the same URL to THIS page

$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);

if( !empty($_POST) && (empty($_POST['name']) || empty($_POST['start_time']) || empty($_POST['end_time'])) ) {
	$msg = "Please check your inputs!";
} elseif(!empty($_POST)) {
	$url = "https://graph.facebook.com/me/events?" . $access_token;
	$params = array();
	// Prepare Event fields
	foreach($_POST as $key=>$value)
		if(strlen($value))
			$params[$key] = $value;
	
	// Check if we have an image
	if( isset($_FILES) && !empty($_FILES['picture']['name']) ) {
		$uploaddir = './upload/';
		$uploadfile = $uploaddir . basename($_FILES['picture']['name']);
		if (move_uploaded_file($_FILES['picture']['tmp_name'], $uploadfile)) {
			$params['picture'] = "@" . realpath($uploadfile);
		}
	}	
	
	// Start the Graph API call
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL,$url);
	/*
		Next option is only used for 
		user from a local (WAMP) 
		machine. This should be removed
		when used on a live server!

https://github.com/facebook/php-sdk/issues/7

	*/
	//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

	curl_setopt($ch, CURLOPT_POST, true);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
    $result = curl_exec($ch);
    $decoded = json_decode($result, true);
    curl_close($ch);
	if(is_array($decoded) && isset($decoded['id'])) {
		// Event created successfully, now we can
		// a) save event id to DB AND/OR
		// b) show success message AND/OR
		// c) optionally, delete image from our server (if any)
		$msg = "Event created successfully: {$decoded['id']}";
	}
}
?>
<!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 enctype="multipart/form-data" action="" method="post">
	<p><label for="name">Event Name</label><input type="text" name="name" value="a" /></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>
Back to top
 

Notes

  • File upload snippet (lines 32-38) is for education purposes only, you should NOT use it on live websites!
  • One can use the PHP-SDK instead of writing the cURL call ourselves, but this snippet is meant to people who don’t want to use the whole SDK for this simple task
  • This code can be easily modified to accomplish other Graph API calls (e.g. posting to user wall, uploading a photo)
Back to top
  • Pingback: How To: Create Facebook Events Using Graph API | API? Yes Master!

  • http://twitter.com/RogerKemp Roger Kemp

    THanks for sharing this code. Is it possible to use this to post an event to a Page? I tried it but I didn’t get it to work. Thanks again.

  • Pingback: How To: Check Status And RSVP To Facebook Events Using Graph API & FQL | API? Yes Master!

  • http://www.facebook.com/people/Dean-Hurley/1038439076 Dean Hurley

    Fantastic work guys… Been trying to get me head round how to access event info for weeks… Do you guys know a way I can then create an FQL query to display all events for the existing user?

  • http://www.facebook.com/grzegorz.jaciubek Grzegorz Jaciubek

    Hi! Sorry for my English, but I have question: How create an event by page?

    I do like this:  $url = “https://graph.facebook.com/me/events?” . $access_token; -> $url = “https://graph.facebook.com/[page_id]/events?” . $access_token;

    But this does not work;/

    • Shameelabdulla

      I am trying to create event in a Facebook Group. But the event is created under my name and not under group. How to create group in a Facebook group. I tried using the following https://graph.facebook.com//events from graph API explorer with following commands name, start_time. Though it return id of a valid event created it is created under my name and not under group.

  • http://www.facebook.com/profile.php?id=100001652651017 Yu Huan Liu

    Hi , I’m trying to create an event , but i follow the step that you wrote….

    but i can’t upload an Image to my event , could you help me????

  • Koen

    Great article! I was able to create events for a group and posting a link on the wall trough the GraphAPI. I’m able to invite people through the old REST-API but although they are invited they don’t get an invitation message (I use ‘personal_message’). Any help on that topic is highly appreciated.

    • http://www.facebook.com/profile.php?id=786517725 Mohamd Itani

      did u add a value for the action attribute  ?

  • http://www.facebook.com/people/Beta-Tester/100002484004360 Beta Tester

    Is there a way to erase events the application created? using this method?

  • LeHHeL

    This will create as a user the events. Now, as a user, I have business page and I want to be created as a business event, can I do this?

  • http://quadnode.com/ Amitesh

    Thanks for great article. I was came to this page when looking for picture upload example for an event in RoR. I thought my finding will help to other, So I am sharing here.

    Here is a sample example using FbGraph[1] written by Nov Matake[2] in Ruby on Rails. I spend lots of hours to figure out, How to upload an image for an event. I hope this will help and save others time.

        me = FbGraph::User.me(ACCESS_TOKEN)
        event = me.event!(
          :name => ‘FbGraph test event’,
          :start_time => 1.week.from_now.to_i,
          :end_time => 2.week.from_now.to_i,
          :picture => File.new(“/home/jeetu/Desktop/my_birthday.png”)
        )

      [1]: https://github.com/nov/fb_graph
      [2]: https://github.com/nov

  • http://www.facebook.com/pr.amrita Prashanth Reddy

    i nead my users to redirect to another page ofter they created the event ,how can i?

  • jems

    Thanks for sharing this its works fine.
    i want to edit my event so how can it possible please reply

  • Manu
    • http://twitter.com/garskiballs Gary Wright

      Hey did you ever figure out why the script gets this error? I am getting it to

  • Debashish jena

    I Used this code for testing create events and set appid app_secret but not get any results…………