How to Use Facebook Graph API 2.X to Get Count for URL Likes, Shares, and Comments
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.
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:
// FACEBOOK GRAPH API LINK
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.
// FACEBOOK GRAPH API for 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.
$url = 'http://www.texashillcountry.com';
$apiUrl = 'https://graph.facebook.com/?ids='.$url;
$ch = curl_init();
//set the url
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$result = curl_exec($ch);
$data = json_decode($result,true);
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.
Evaluating Expired Domains using Facebook Graph API’s Count of Likes, Shares, and Comments for URL
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.