Truemag

  • Categories
    • Tips And Tricks
    • Internet
    • PHP
    • Javascript
    • CSharp
    • SQL Server
    • Linux
  • Lastest Videos
  • Our Demos
  • About
  • Contact
  • Home
  • Write With Us
  • Job Request
Home Facebook Graph API PHP Get Facebook URL Total Comments, Total Likes, Total Shares

PHP Get Facebook URL Total Comments, Total Likes, Total Shares

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>

<?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.

Feb 22, 2012Hoan Huynh

Source Code Demo page

ASP.NET Get Facebook URL Total Comments, Total Likes, Total SharesAuto Rotate Web Page Title With JavaScript
You Might Also Like:
  • Get Facebook Total Comments, Total Likes, Total Shares Of A Given URL
  • ASP.NET Get Facebook URL Total Comments, Total Likes, Total Shares
  • PHP Get Likes Number Of Facebook Page
  • Count Total Rows For All Tables In MS SQL Server
  • Get WordPress Most Popular Posts By Total Comments
  • Facebook Like Button And Recommend Button With fb:like, iframe and html5
  • Facebook Load User Profile Via Graph API And FQL Query
  • PHP Check If User Like Facebook Page
  • Load And Save Facebook Profile Picture Of User
  • PHP Load Facebook Albums And Save To MySQL Database
Hoan Huynh

Hoan Huynh is the founder and head of 4rapiddev.com. Reach him at [email protected]

9 years ago Facebook Graph APIFacebook, FQL, link_stat, links.getStats, restserver, sizeof352
0
GooglePlus
0
Facebook
0
Twitter
0
Digg
0
Delicious
0
Stumbleupon
0
Linkedin
0
Pinterest
Most Viewed
PHP Download Image Or File From URL
22,198 views
Notepad Plus Plus Compare Plugin
How To Install Compare Text Plugin In Notepad Plus Plus
20,070 views
Microsoft SQL Server 2008 Attach Remove Log
Delete, Shrink, Eliminate Transaction Log .LDF File
15,848 views
JQuery Allow only numeric characters or only alphabet characters in textbox
13,327 views
C# Read Json From URL And Parse/Deserialize Json
9,819 views
4 Rapid Development is a central page that is targeted at newbie and professional programmers, database administrators, system admin, web masters and bloggers.
Recent Posts
  • Free Online Photo Editor
  • Easy Tips For Writing An Essay
  • What Can I Expect From An Academic Essay Service?

  • Can Be Essay Service Companies Good For You?

  • Tips To Choosing The Ideal Essay Writers
Categories
  • CSharp (45)
  • Facebook Graph API (19)
  • Google API (7)
  • Internet (87)
  • iPhone XCode (8)
  • Javascript (35)
  • Linux (28)
  • MySQL (16)
  • PHP (84)
  • Problem Issue Error (29)
  • Resources (32)
  • SQL Server (25)
  • Timeline (5)
  • Tips And Tricks (141)
  • Uncategorized (112)
Recommended
  • Custom Software Development Company
  • Online Useful Tools
  • Premium Themes
  • VPS
2014 © 4 Rapid Development