PHP Code to Get Facebook Like, Share and Comment Count

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 .

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

 

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

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<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>

Facebook Share Count

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<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>

 

Facebook Comment Count

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<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>

 

 

Facebook Comment Box Count

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<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>

About prasad k

Prasad Kuppusamy has written 81 post in this blog.

i m php developer, web designer.

5 Responses to PHP Code to Get Facebook Like, Share and Comment Count

  1. [...] reference : http://freezecoders.com/2012/09/php-code-to-get-facebook-like-share-and-comment-count.html link [...]

  2. BHARAT GAANGADIYA says:

    HOW TO COUNT.. HOW MANY PEOPLE VISIT OUR SITE

  3. black says:

    How can I get the share count of a photo on Facebook?

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

aspersive-replow