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><labelfor="name">Event Name</label><input type="text"name="name"value=""/></p><p><labelfor="description">Event Description</label><textarea name="description"></textarea></p><p><labelfor="location">Location</label><input type="text"name="location"value=""/></p><p><labelfor="">Start Time</label><input type="text"name="start_time"value="<?php echo date('Y-m-d H:i:s'); ?>"/></p><p><labelfor="end_time">EndTime</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><labelfor="picture">Event Picture</label><input type="file"name="picture"/></p><p><labelfor="privacy_type">Privacy</label><input type="radio"name="privacy_type"value="OPEN"checked='checked'/>Open <input type="radio"name="privacy_type"value="CLOSED"/>Closed <input type="radio"name="privacy_type"value="SECRET"/>Secret </p><p><input type="submit"value="Create Event"/></p></form></body></html>
Our Facebook event form
Explanation
Here we are:
- Getting the user
access_token - Preparing the Graph API link we are posting to
$event_url - 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_eventpermission - Instead of the
privacyfield useprivacy_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_timeandend_timefields to get the expected dates (as recommended by Facebook) - While it is not documented, specifying the
picturefield 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
Post a Comment