Skip to main content

How To: Create Facebook Events Using Graph API – Advanced


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>


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)


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 Apple iPad interface.

30 Free Facebook Timeline Cover Backgrounds

If you really want a good cover for your Facebook Timeline, you have landed on the right page. It’s seen that people have faced problem in finding covers of their choice because of the specific size issue. However, you do not need to look further, as this list presents 30 cool free Facebook Timeline covers for you to sport them on your profile page. The list includes creative, funny, exciting and cool pictures of perfect dimensions to fit your Facebook cover without having you to crop or resize them. Moreover, they are absolutely free to download, so you can instantly put them on your Timeline cover to express numerous moods and inspirations. If you like this article, you might be interested in some of our other articles on  Thumbs Up Symbol,Heart Symbol On Facebook, Facebook Timeline Covers and Facebook Timeline As Used By Brands . Love Calculator What all it takes to result in love? A pretty woman and drinks. Love Calculator This Guy Rocks Let your cover boast about you and tell your

PHP File Upload

With PHP, it is possible to upload files to the server. Create an Upload-File Form To allow users to upload files from a form can be very useful. Look at the following HTML form for uploading files: <html> <body> <form action="upload_file.php" method="post" enctype="multipart/form-data"> <label for="file">Filename:</label> <input type="file" name="file" id="file" /> <br /> <input type="submit" name="submit" value="Submit" /> </form> </body> </html> Notice the following about the HTML form above: The enctype attribute of the <form> tag specifies which content-type to use when submitting the form. "multipart/form-data" is used when a form requires binary data, like the contents of a file, to be uploaded The type="file" attribute of the <input> tag specifies that the input sho