How to Use Facebook Graph API 8.X to Retrieve Engagement Activity

Welcome to today’s tutorial where I reveal how to retrieve engagement activity using cURL, PHP, and Facebook’s Graph API version 8.

You may or may not recall the version JS, version 2, and version 3 tutorials I produced a few years ago. Since this time, I’ve been quite busy with a myriad of other projects, and completely missed 4, 5, 6, and 7 version updates to the Facebook Graph API.

Nonetheless, I aim to help you obtain your access token and facebook engagement activity using the latest Graph API v8. Let’s get started!

How to obtain access token using Graph API Explorer

If you haven’t already done so, head over to Facebook for Developers and create your app.

You’ll need either a test or live app to be able to retrieve engagement activity from Facebook’s Graph API. Without an app, you’re somewhat dead in the water.

Once you have your created, head over to the Facebook for Developers Graph API Explorer see image below. While possible to  programmatically request your auth token (highly recommended), it’s just as easy to use the web interface for this tutorial to obtain a token.  Note that the web interface token will expire after 30 minutes or more, so read up on the OAuth 2.0 protocol.

How to obtain access token using Graph API Explorer

In the right hand side of the page, you should see an Access Token column. Before clicking the Generate Access Token button, be sure to do the following:

  • Select the respective Facebook App from the dropdown
  • Select User Token from the User or Page dropdown

Once both selections have been made, simply click the Generate Access Token to create your Graph API access token. Copy this token to a text file for safe keeping in case you have to refer back to it.

How to retrieve engagement activity using cURL, PHP, and Facebook’s Graph API Version 8

Now that you have a valid access token, we’re ready to generate the code helping us to unlock and discover the engagement activity — reaction, comment, and share counts — for a given url or web address.

You’ll need to create a PHP and save a PHP file named fbv8.php. You can name your file whatever you would like. I simply named my file fbv8.php.

Next, define the following variables:

  • $accessToken – the value of the variable should be set to the value of the Graph Access Token (see previous section using the Facebook for Developers Graph API Explorer).
  • $url – the value of the variable should be set to the url or web address of desired engagement activity.
  • $fields – the value of the variable should be set to engagement (refer to Facebook Developer Documentation for more insight about fields).

Now that variables are defined with their respective values, it’s time to create the full url, with query parameters, to be used when making the Graph API request. To do so, create a params variable and set its value to be an array containing the following key-value binding(s):

  • id => $url
  • fields => $fields
  • access_token =>$accessToken

Next, the params variable will be pass as the first argument to the built-in PHP method http_build_query. This method essentially prepares and builds the encoded query string — simply put, less manual intervention and opportunity for error in attempting to handcraft a url query of parameters.

In addition, feel free to refer to the following cookbook for PHP should you want to dive deeper into the detail and discover more creative uses:

And you guessed it! Next, create a urlparams variable and define it’s to be the http_build_query method. Set the method arguments as follows:

  • Set first argument to be the $params variable
  • Set second argument to be ‘’ (two single quotes)
  • Set third argument to be & (ampersand)

Now that we have the query parameters defined and query string built, the next variable to define is the Facebook for Developers Graph API url for version 8, which we will define as the baseurl variable. It is as follows: Be sure to include the ? mark at the very end. Concatenating both baseurl and urlparams, create a fullurl variable.

Once the fullurl variable is successfully created, it’s time to make the cURL call using PHP. Be sure to set the CURLOPT_URL option to be $fullurl. I’m not going to bore you with the details of this API request/call, but feel free to refer to tutorials version JS, version 2, and version 3 to discover and learn how to display data from the request/call response object.

When the code below is opened and executed in a web browser, it should display the following (don’t forget to add your access token):

In closing, this is all it takes to make a Facebook Graph API request to retrieve engagement activity using cURL, PHP, and version 8 of Facebook’s Graph API.

Please don’t hesitate to contact should you encounter technical challenges. Also, we would love to hear from you should this tutorial have helped you successfully retrieve Facebook Graph API data.

Thanks and enjoy the code!


Written by Alvin Brown
He's an experienced and passionate serial entrepreneur, founder and publisher of Kickstart Commerce. Alvin possesses a great love for startups dominating their market using profitable digital strategies for greater commerce.