
How to Use Facebook Graph API 2.X to Get Count for URL Likes, Shares, and Comments
Quick Summary of Contents
A few weeks ago, one of my many websites crossed the thousand like club for Facebook likes.
It was a time to celebrate! Why?
Well, this particular website is a local and somewhat a hyper micro-niche. To be honest, I’m surprised and elated this website has 1000+ likes.
Nevertheless, crossing the 1000+ Facebook like club renders the like button differently.
Instead of being able to now see that there are 1,024 likes, the Facebook like button for this specific website now only shows “1K”.
And because I don’t have a Facebook Fan, Business, or Profile page associated with this website, this adds yet another degree of difficulty in attempting to understand exactly how many likes, shares, and comments exist for website URL, also known as website domain name.
Of course, I Googled a bit to see if there were any quick ideas or methods for retrieving total count for URL likes, shares and comments.
However, most of the answers Googlged were outdated using Facebook’s old REST and FQL API services.
Nevertheless, I ventured over to Facebook for Developers and read the Graph Documentation, which led me to the simple solution I’m about to share with you.
Making Facebook Graph API Call to Retrieve URL Data
The good part about this solution is that you don’t need to be logged into Facebook to use it. Woohoo!
All you need is a web browser, and url appended to the following link:
1 2 3 |
// FACEBOOK GRAPH API LINK https://graph.facebook.com/?ids=website-url-here |
For example, viewing Texas Hill Country’s Facebook page reveals that they have 972,230 likes. Again, this is their Facebook page and not their url.
I wonder if we’ll find the same number if we were to look at how many likes the website url or domain name has.
To figure this out, I appended Texas Hill Country’s url, http://www.texashillcountry.com, to the aforementioned link and found that it has 15,240 shares/likes. That’s certainly a ways different than their Facebook page.
1 2 3 |
// FACEBOOK GRAPH API for TexasHillCountry.com https://graph.facebook.com/?ids=http://www.texashillcountry.com |
Nevertheless, your web browser should return something similar to the following image:
When executing the link in a web browser, the Graph API returns a JSON object to the screen.
Notice the “comment_count” and “share_count” attributes listed as object attributes associated to the “share” attribute.
The “comment_count” attribute refers to the total number of comments a URL or domain has received, while the “share_count” attribute refers to the total number of shares/likes a URL or domain has received.
The JSON object also includes other attribute data such as id, description, title, type, and last update time.
PHP / Facebook Graph API Call to Retrieve and Display URL Data
Of course, I know some of you would like to access the Facebook Graph API data for a given URL.
So, the code below shows how to retrieve and display the URL data using PHP and PHP’s built-in curl method.
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 |
<?PHP $url = 'http://www.texashillcountry.com'; $apiUrl = 'https://graph.facebook.com/?ids='.$url; //open connection $ch = curl_init(); $timeout=5; //set the url curl_setopt($ch,CURLOPT_URL, $apiUrl); 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); //execute post $result = curl_exec($ch); //close connection curl_close($ch); $data = json_decode($result,true); echo '<pre>'; print_r($data); echo '</pre>'; echo '<p>Likes: '.number_format($data[$url]['share']['share_count']).'</p>'; ?> |
Included in the code, I show how to access the multi-dimensional PHP Array and the exact data point for total number of likes, rather the “share_count” attribute.
Here’s what the code above should look like when executed via a web browser:
Although I’ve not looked or searched the web, there is likely a blog plugin or adapter for your website’s specific content management system (CMS) that would simply the code above to a few point and clicks.
Nevertheless, you now know how to retrieve a URL’s total count for likes using PHP and Facebook Graph API, or simply using the Facebook Graph API url with domain appended via web browser.
Not only do I check Facebook Graph stats for websites I develop and manage from time to time, I also use this stat as one of many factors to help me to decide whether or not to bid on expired domain auctions.
If you didn’t know, Google values social signals for domain names. With hundreds of thousands of domains expiring daily, I use Facebook Graph stats to help me gauge my bidding interest for domains I’m on the fence about actively bidding.
I’ve also found expired domain gems with a trove of Facebook likes that I managed to snipe or purchase for BIN or closeout prices.
To learn more about how to use Facebook Graph API when evaluating expired domains, please watch my quick 5 minute tutorial below.
Also, please don’t hesitate to send me questions or comments about any of the topics I’ve mentioned above.
hi
This code is working facebook share count is display i tested more time.
now got this error message
can you please how to solve this error
{“error”:{“message”:”(#4) Application request limit reached”,”type”:”OAuthException”,”is_transient”:true,”code”:4,”f
Yes, this code doesn’t working anymore. You should follow this link structure in order to work properly. https://developers.facebook.com/docs/graph-api/reference/v3.1/url 🙂 Enjoy!
Thanks Lakovos! I’ll be sure to update the code to the current graph version although the presented method DOES work. Thanks again.
Thanks and congrats Alvin, the code is completely working from my side
Thanks Mariano, and glad the code worked for you! 🙂
Hi Alvin,
It looks like the counts aren’t available anymore because of privacy concerns.
The url does not return any share count. I am not a programmer, so is there another -simple- way to get the likes for my website?
Kind regards,
Jack
Well, that’s a bummer Jack! Hmm… let me see if I can find another way (doesn’t look promising though).
Jack – Good news! I produced an updated tutorial. The write up will be delivered by May 1st.
Is it possible to get the view count and the URL of these videos and pull them in Excel file ?
Yes, is is possible to retrieve engagement info and write data to an excel file (example tutorial writing xml to excel/csv file).