PHP Code to Get Facebook Like, Share and Comment Count

By | September 2, 2012

Facebook like, comment and share count can be get through facebook buttons, which are provided by facebook. Here i am going to explain how to get the facebook like, share, comment and comment_box count using php script.

Facebook Share Count

By using this URL http://graph.facebook.com/, we can get facebook like and share count. But this will return the sum of likes and shares of the URL. It won’t return like and share count seperately, the following code will give counts seperately .

[code type=php]

<html>
<form action=”” method=”post”>
Enter Your URL: <input type=”text” name=”url” />
<input type=”submit” name=”submit” />
</form>
<?php
if(isset($_POST[‘submit’]))
{
$url1=$_POST[‘url’];
$src = json_decode(file_get_contents(‘http://graph.facebook.com/’ . $url1));
$share_count = $src->shares;
echo “Total number of shares and likes for your url: <b>”.$share_count.”</b>”;
}

?>
</html>

[/code]

 

Facebook like, share, comment and comment box counts are get through api.facebook.com. By using get_file_contents() and html_entities() functions page source of the webpage will be stored in a variable. Using strpos() functions <like_count>, <share_count> and <comment_count>

Facebook Like Count

[code type=html]

<html>
<form method=”post” action=””>
Enter URL: <input type=”text” name=”url”>
<input type=”submit” name=”submit”>
</form>
<?php
if(isset($_POST[‘submit’]))
{
$url1=$_POST[‘url’];
$addr=”http://api.facebook.com/restserver.php?method=links.getStats&urls=”.$url1;
$page_source=file_get_contents($addr);
$page = htmlentities($page_source);
$like=”<like_count>”;
$like1=”</like_count>”;
$lik=strpos($page,htmlentities($like));
$lik1=strpos($page,htmlentities($like1));
$fullcount=strlen($page);
$a=$fullcount-$lik1;
$aaa=substr($page,$lik+18,-$a);
$aaa1=substr($page,605,610);
echo “Like Count:  “.$aaa;
}
?>
</html>

</html>

[/code]

Facebook Share Count

[code type=php]

<html>
<form method=”post” action=””>
Enter URL: <input type=”text” name=”url”>
<input type=”submit” name=”submit”>
</form>
<?php
if(isset($_POST[‘submit’]))
{
$url1=$_POST[‘url’];
$addr=”http://api.facebook.com/restserver.php?method=links.getStats&urls=”.$url1;
$page_source=file_get_contents($addr);
$page = htmlentities($page_source);
$like=”<share_count>”;
$like1=”</share_count>”;
$lik=strpos($page,htmlentities($like));
$lik1=strpos($page,htmlentities($like1));
$fullcount=strlen($page);
$a=$fullcount-$lik1;
$aaa=substr($page,$lik+19,-$a);
$aaa1=substr($page,605,610);
echo “Share Count: “.$aaa;
}
?>
</html>

[/code]

 

Facebook Comment Count

[code type=php]

<html>

<form method=”post” action=””>
Enter URL: <input type=”text” name=”url”>
<input type=”submit” name=”submit”>
</form>
<?php
if(isset($_POST[‘submit’]))
{
$url1=$_POST[‘url’];
$addr=”http://api.facebook.com/restserver.php?method=links.getStats&urls=”.$url1;
$page_source=file_get_contents($addr);
$page = htmlentities($page_source);
$like=”<comment_count>”;
$like1=”</comment_count>”;
$lik=strpos($page,htmlentities($like));
$lik1=strpos($page,htmlentities($like1));
$fullcount=strlen($page);
$a=$fullcount-$lik1;
$aaa=substr($page,$lik+21,-$a);
$aaa1=substr($page,605,610);
echo “Comment Count: “.$aaa;
}
?>
</html>

[/code]

 

 

Facebook Comment Box Count

[code type=php]

<html>
<form method=”post” action=””>
Enter URL: <input type=”text” name=”url”>
<input type=”submit” name=”submit”>
</form>
<?php
if(isset($_POST[‘submit’]))
{
$url1=$_POST[‘url’];
$addr=”http://api.facebook.com/restserver.php?method=links.getStats&urls=”.$url1;
$page_source=file_get_contents($addr);
$page = htmlentities($page_source);
$like=”<commentsbox_count>”;
$like1=”</commentsbox_count>”;
$lik=strpos($page,htmlentities($like));
$lik1=strpos($page,htmlentities($like1));
$fullcount=strlen($page);
$a=$fullcount-$lik1;
$aaa=substr($page,$lik+25,-$a);
$aaa1=substr($page,605,610);
echo “<br />Comment Box Count: “.$aaa.”<br /><br /><br /><br /><br /><br />”;
}
?>
</html>

[/code]

7 thoughts on “PHP Code to Get Facebook Like, Share and Comment Count

  1. Pingback: PHP đếm số người like bài viết, share, đếm số comments | Tất cả dành cho người làm Web

Leave a Reply

Your email address will not be published. Required fields are marked *

mildly-noncreativity