24 March, 2023

How To Import Events From Facebook

Facebook has given a synchronization feature to import events to your calendar. This greatly helps to retrieve the overall events created by the users in their respective accounts.These features are explained widely in the following sections.

Retrieving Access token

Usually while importing events from Facebook, developers need access token from Facebook. After getting the access token only, one can proceed further with the importing events, etc. Hence Facebook gives it to authorize the import.

CURL (Client URL Request library) is the library which allows you to connect and communicate to many different types of servers with many different types of protocols.

Here are few steps to get it using CURL method:

  1. Create an app in http://developers.facebook.com and supply all the credentials such as

                            (I) App Domain – only domain name (for eg : example.com)

                            (II) Site URL – return url where you get the code

                            (III) Canvas URL – same as siteurl (note: put a slash at the end)

                            (IV) Secure Canvas URL – same as siteurl (note: provide https, put a slash at the end)

2. After creating an app you get the App ID and App Secret. Save them in your database through your admin interface or any other way which you prefer.

3. Download facebook API from https://github.com/facebook/facebook-php-sdk, where you will also get the fb bundle certificate which will be used for curl.

4. Now create a file and paste the php code in it.

                         $app_id = ‘APP_ID’;

                         $app_secret = ‘APP_SECRET’;

                         $return_url = ‘YOUR_RETURN_URL’;

                        $dialog_url = “http://www.facebook.com/dialog/oauth?client_id=”.$app_id . “&redirect_uri=”.urlencode($return_url).”&scope=email,read_stream,publish_stream”;

                        <a href=”<?php echo $dialog_url; ?>”>Connect to facebook</a>

Now a link connect to facebook redirects to login page of facebook where you can create an event that can be synchronized later.

5. Now create another file which is the return url page.   for ex:(return.php).Paste the following code:

                         $app_id = ‘APP_ID’;

                         $app_secret = ‘APP_SECRET’;

                         $return_url = ‘YOUR_RETURN_URL’;

                         //You will get this from redirect page as a query parameter

                         $code = JRequest::getVar(‘code’);

                          $cert = “../facebook /fb_ca_chain_bundle.crt“; //provide the path where you have stored the facebook API

                          $token_url = “https://graph.facebook.com/oauth/access_token?client_id=”. $app_id . “&redirect_uri=” . urlencode($return_url) . “&client_secret=”. $app_secret . “&code=” . $code.”&scope=email,read_stream,publish_stream”;

                          //Getting access_token using curl method

                          $c = curl_init($token_url);

                          curl_setopt($c, CURLOPT_HTTPGET, true);

                          curl_setopt($c, CURLOPT_FRESH_CONNECT, true);

                           curl_setopt($c, CURLOPT_RETURNTRANSFER, true);

                           curl_setopt ($c, CURLOPT_SSL_VERIFYPEER, TRUE);

                            curl_setopt ($c, CURLOPT_CAINFO, $cert);

                            $output = curl_exec($c);

                            if ($output === false) {


                                   return false;



                              $curltoken = explode(‘&’,$output);

                              $token = explode(‘=’,$curltoken[0]);

                              $access_token = $token[1]; //get access token


                                 (i) Replace the appid, appsecret and return url with your credentials received from FB after creating an app.

                                 (ii) Check whether CURL is enabled in your server.

Now the Access token is ready for further continuation of your importing process. 

Importing Events using Facebook API

1. After getting the access token,you can import created events from Facebook. Now you create a synchronise button and execute the below function

                           $appid = ‘APP_ID’;

                           $secret = ‘APP_SECRET’;

                           $token = ‘ACCESS_TOKEN’;

                           if (empty($token)){

                              here redirect back to the page throwing error “No Facebook Connection”…


                             //Facebook API function to get the event details from FB

                            $facebook = new Facebook(array(‘appId’ => $appid, ‘secret’ => $secret, ‘cookie’ => true));

                            $attachment = array(‘access_token’ => $token,’limit’ => 100);

                            $container = array();



                                  $user = $facebook->api(‘/me’, ‘GET’, $attachment);

                                  $events = $facebook->api(‘/me/events’, ‘GET’, $attachment);

                                  if (!empty($events) && !empty($events[‘data’]))


                                       foreach ($events[‘data’] as $event)


                                            $objResult = $facebook->api($event[‘id’], ‘GET’, $attachment);

                                             if (!empty($objResult)){

                                             $fevent = new stdClass();

                                             $fevent->id = $objResult[‘id’];

                                             $fevent->EventName = $objResult[‘name’];

                                             $fevent->EventDescription = $objResult[‘description’];

                                             $fevent->EventStartDate = isset($objResult[‘start_time’]) ? strtotime($objResult[‘start_time’]) : time();

                                             $fevent->EventEndDate = isset($objResult[‘end_time’]) ? strtotime($objResult[‘end_time’]) : time() + 7200;

                                             $fevent->EventHost = isset($objResult[‘owner’][‘name’]) ? $objResult[‘owner’][‘name’] : ”;

                                              $fevent->Location = isset($objResult[‘location’]) ? $objResult[‘location’] : ”;

                                               $fevent->LocationStreet = isset($objResult[‘venue’][‘street’]) ? $objResult[‘venue’][‘street’] : ”;

                                                $fevent->LocationCity = isset($objResult[‘venue’][‘city’]) ? $objResult[‘venue’][‘city’] : ”;

                                                $fevent->LocationState = isset($objResult[‘venue’][‘state’]) ? $objResult[‘venue’][‘state’] : ”;

                                                $fevent->LocationCountry = isset($objResult[‘venue’][‘country’]) ? $objResult[‘venue’][‘country’] : ”;

                                                 $fevent->LocationLat = isset($objResult[‘venue’][‘latitude’]) ? $objResult[‘venue’][‘latitude’] : ”;

                                                  $fevent->LocationLon = isset($objResult[‘venue’][‘longitude’]) ? $objResult[‘venue’][‘longitude’] : ”;

                                                   $eventList[] = $fevent;





                            catch (Exception $e)


                              here redirect back to the page throwing Exception error – $e->getMessage()…


2. From the above method you will be able to get the details of the events in an array which you have created in Facebook. Using this details, you can save it in the database.

                            if (!empty($eventList)){ //array of list obtained from the previous step

                                   foreach ($eventList as $event)


                                        Write the sql queries here to save the details in database……



This is the methodolody for importing events from Facebook. Hope this could be very useful for you in importing events. Please do send queries if you need any further clarification.

Alex Sam is a digital marketer by choice & profession. He munches on topics relating to technology, eCommerce, enterprise mobility, Cloud Solutions and Internet of Things. His other interests lies in SEO, Online Reputation Management and Google Analytics. Follow Me Socially: Habr , Dev.to & Viblo .


  1. alex Reply


    I have created a public event in my facebook account and then used the above code according to the instructions. i received the complete user detail but didn’t receive any event, please advice on it what i had miss. is there any settings at facebook end.

Leave a Reply

Your email address will not be published. Required fields are marked *