inner-banner

Blog

Home Blog

How To Import Events From Facebook Using Graph API?

October 20, 2017 | Sujit Shinde |

img

Are you looking to import facebook events using graph API? Using Graph API we can fetch public events and display on our application. In this article, I will guide you how to write a code to fetch Facebook events using graph API.

 

Register An Application 

To get started, we need to register an application on Facebook Developer. You can find step by step guide on registering application here

Once you created an application, copy the app id and app secret which will require in a moment. 

App id and app secret keys are used to generate access token. Access token is an identifier for your application which interact with the graph APIs. By using the access token, graph API identifies from which application request is coming and whether request is valid or not.

 

Generate Access Token

There are 2 types of access token short-lived and long-lived. Short-lived token as per the name expire in limited time. Long-lived token is a better choice comapre to short one. As it has a more lifetime compare to short-lived. Generally it has a life period of 60 days. 

You can get the detail guide on generating long-lived token on this article . Please go through this article and create a long-lived token. This token we will use to import facebook events.

 

Import Facebook Events

Now we are ready with our access token and we have registered our application of Facebook. 

Next step is import the events using CURL and graph API. 

Make sure CURL is enabled on your server. If it is not enabled, our code will not execute and gives us a expected result. To fetch the events we need to give a call to the endpoint

you have to generate one url and store that url into a variable like this

 

$generated_url = 'https://graph.facebook.com/search?fields=id&q='.{location_name}.'&type=event&access_token='.{access_token}; 

and then you have to pass that variable to your curl code.

 

Here is the example for curl: 

function function_name($generated_url){

$ch = curl_init();

curl_setopt_array($ch, array(

CURLOPT_URL => $generated_url,

CURLOPT_RETURNTRANSFER => true,

CURLOPT_POST => false,

));

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

$arrJsonOutput = curl_exec($ch);

$arrJsonData = json_decode($arrJsonOutput);

curl_close($ch);

}

 

Using this curl code you will get the data of facebook events and then return the that data to your generated url.

By following this you will be successful in importing the events as per the locations.

About the author

blog-author

Sujit Shinde

A Tech Savy who loves to code Laravel and share knowledge through blogs :)