
How to Use Facebook Graph API 3.X to Retrieve Engagement Activity
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.
This given reference page shares different HTTP, PHP, JavaScript, Android, iOS and Graph API Explorer examples to make requests to retrieve and read data.
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
<?PHP $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"; //open connection $ch = curl_init(); $timeout=5; //set the url curl_setopt($ch,CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET'); // Values: GET, POST, PUT, DELETE, PATCH, UPDATE //execute post $result = curl_exec($ch); //close connection curl_close($ch); $data = json_decode($result,true); echo '<pre>'; print_r($data); echo '</pre>'; // 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!
Thank you, very well written and useful article. I have a question since the way I was doing it is now broken.
Could you provide an example to do it via Javascript – therefore client side – hence without exposing the appsecret, if there’s a way?
Hi there! Have you read or tried using Facebook’s Javascript SDK? Facebook provides a Javascript SDK example for engagement activity. Hope this helps.
Thanks for this article!
Any clue about how to deal with the Facebook quota? An app responsible of getting engagenement stats might perform a lot of calls, while having very few (none?) Facebook users. And the quota limit is very low: 200 requests per user and per day…
Hi Philippe – Thanks for your comment. As for your Facebook quota question, I’d have to investigate further as I’m not too familiar with it.
Hi Alvin Brown this is surendhar
and i need how to get engagement details performance report in facebook using php
Hi
i want to add some more details like counts , views counts , post click counts, on click shares counts, on comments click counts, (performance of facebook details) send me full source code an demo also
Hi Surendhar – Thanks for your message. You should be able to use this tutorial (code example) as a foundation to modify and retrieve the desired details you mention. You’ll need to become quite familiar with Facebook API documentation.
Thanks a lot! This is the one real article about how do get that info.
Great Mikayel! I’m glad you found it helpful and insightful.
Hello, great method, but for 2 weeks it does not work anymore.
Have you got a solution to make it work again ?
With an ajax request and parsing the answer of :
https://graph.facebook.com/v8.0/?id=https://example.com
I was able to get the number of likes.
Thanks.
Hi Laurent! Indeed, it’s about time to update this tutorial to use Graphi version 8. Thanks for the reminder. 🙂