This article will use PHP & Facebook Graph API & FQL to query link_stat Facebook table to get total comments, comment count, total likes, like count, total shares, share count, total count, click count, comment box count of one or more URLs that users on Facebook are interacting with.
We can also get the Share Statistics directly from the browser. However, getting those number on the server side by using PHP or ASP.NET is a good solution if you want to store the numbers in database or cache somewhere for certain purposes. For example, improve your Facebook app performance or allow users to sort or filter entries by the most comments, shares, likes, clicks.
PHP Get Facebook URL Total Comments, Total Likes, Total Shares
<?php require 'src/facebook.php'; require 'config.php'; $facebook = new Facebook(array( 'appId' => $appId, 'secret' => $secret, )); $urls = ""; if(!isset($_POST["txtURLs"]) || $_POST["txtURLs"] == "") { $urls = "http://4rapiddev.com,http://4rapiddev.com/facebook-graph-api/get-or-find-facebook-profile-id-number/"; } else { $urls = $_POST["txtURLs"]; } $raw_urls = "http://api.facebook.com/restserver.php?method=links.getStats&urls=" . urlencode($urls); $url_arr = explode(",",$urls); $query_urls = ""; for($i = 0; $i < sizeof($url_arr); $i ++) { $query_urls .= "'" . $url_arr[$i] . "',"; } $query_urls = substr($query_urls,0,strlen($query_urls)-1); $links = $facebook->api(array( "method" => "fql.query", "query" => "SELECT url, normalized_url, share_count, like_count, comment_count, total_count, commentsbox_count, comments_fbid, click_count FROM link_stat WHERE url in (" . $query_urls . ")" )); $total_links = sizeof($links); $str = ""; for($i=0;$i<$total_links;$i++) { $str .= "url: " . $links[$i]["url"] . "<br>"; $str .= "normalized_url: " . $links[$i]["normalized_url"] . "<br>"; $str .= "share_count: " . $links[$i]["share_count"] . "<br>"; $str .= "like_count: " . $links[$i]["like_count"] . "<br>"; $str .= "comment_count: " . $links[$i]["comment_count"] . "<br>"; $str .= "total_count: " . $links[$i]["total_count"] . "<br>"; $str .= "commentsbox_count: " . $links[$i]["commentsbox_count"] . "<br>"; $str .= "comments_fbid: " . $links[$i]["comments_fbid"] . "<br>"; $str .= "click_count: " . $links[$i]["click_count"] . "<br>"; $str .= "<hr>"; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <form method="post" action="php-get-facebook-url-total-comments-total-likes-total-shares.php" id="form1"> <div> <table> <tr> <td valign="top" align="left">URLs: </td> <td align="left"><input name="txtURLs" type="text" value="<?php echo $urls;?>" id="txtURLs" style="width:300px;" /> (separated by comma)<br /> + http://4rapiddev.com<br /> + http://4rapiddev.com,http://4rapiddev.com/facebook-graph-api/get-or-find-facebook-profile-id-number/</td> </tr> <tr> <td></td> <td><input type="submit" name="btnSubmit" value="Submit" id="btnSubmit" /></td> </tr> <tr> <td colspan="2"><hr /></td> </tr> <tr> <td colspan="2"><strong>Request URL</strong></td> </tr> <tr> <td colspan="2"><span id="lblRequestURL"></span></td> </tr> <tr> <td colspan="2"><strong>Raw result from Facebook</strong></td> </tr> <tr> <td colspan="2"><a id="hplRawResult" href="<?php echo $raw_urls; ?>" target="_blank">Click here</a></td> </tr> <tr> <td colspan="2"><strong>Facebook Share Statistic Information</strong></td> </tr> <tr> <td colspan="2"><?php echo $str;?></td> </tr> </table> </div> </form> </body> </html> |
Note:
- 1. In order to specify more than one URL, you must use the IN operator in the query’s WHERE clause.
- 2. You should validate input URL before querying.
- 3. You have to update appId and secret in config.php file with your information
+ [download id=”10″ format=”1″]
+ View our demonstration page to see how it works.