It’s hard to believe a few years has passed since I wrote the tutorial about how to use Facebook’s Graph API 2.X to retrieve the count for a given URL’s likes, shares, and comments.
I was informed a few days ago that my seamless solution for displaying the total count for likes, shares, and comments is dead in the water and no longer works.
Well, I guess this means we’re all in the dark now since Facebook’s most recent API upgrade doesn’t allow for such desired functionality, right?
Wrong! Facebook’s latest upgrade to Graph API actually requires a more secure approach to retrieving data. Allow Graph API 2.X was easy to use when attempting to retrieve engagement activity, it was less secure.
Facebook’s Graph API 3.X is more secure and robust, although you’ll have to do a bit more math to retrieve the overall total likes for a given url or object.
In today’s “upgrade” tutorial, I’ll build upon the codebase I shared with you in my last tutorial.
But first things first. You must create an App to be able to use the Facebook Graph API. Head on over to Facebook for Developers.
Once you create your app and it’s approved, then you’ll need the App ID and App Secret. Be sure not to share the App Secret publicly.
Once you have an App ID and App Secret, you’re now ready to do a bit of reading up in the Graph API Reference for developers. Read about how to retrieve engagement activity for a given URL external to Facebook.
In the aforementioned reference for the URL, the page also shares the necessary permissions needed as well as the following fields and their respective key-pair values that can be returned when a request is made.
- app_links – AppLinks data associated with this URL, if available
- id – the URL itself
- engagement – counts of different ways (i.e., reaction_count, comment_count, share_count, comment_plugin_count) people interacted with the URL.
- og_object – The Open Graph Object that is canonically associated with this URL
Making Facebook Graph API Call to Retrieve Engagement Activity
To make the request to retrieve engagement activity for a URL, I’m modifying the previous code base. Really, the first 4 and last 4-6 lines of code are modified.
For the sake of time, I’ve included the code below. Be sure to replace your-app-id, your-app-secret and the first url variable’s value with the desired URL.
$appid = 'your-app-id';
$appsecret = 'your-app-secret';
$url = 'http://www.batsinaustin.com';
$url = "https://graph.facebook.com/v3.2/?id=$url&fields=engagement&access_token=$appid|$appsecret";
$ch = curl_init();
//set the url
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET'); // Values: GET, POST, PUT, DELETE, PATCH, UPDATE
$result = curl_exec($ch);
$data = json_decode($result,true);
// add up engagement numbers: reaction count, comment count, and share count
$total_count = $data['engagement']['reaction_count'] + $data['engagement']['comment_count'] + $data['engagement']['share_count'];
echo '<p>Likes: '.number_format($total_count).'</p>';
Once you do this, save the code in a php file on a web server that supports PHP, and view the file using a web browser. It should display the following:
And for those that thought Facebook’s Graph API 3.X upgrade buried the ability and functionality to retrieve engagement activity for URLs and web pages, you better think again, because you have another think coming.
For now, tune in and watch my quick tutorial as I explain and walk through the codebase and process in its entirety.
In closing, please don’t hesitate to send me questions or comments about any of the topics I’ve mentioned in this tutorial. Thanks and that’s all for now!