Thursday 7 April 2016

How to Count Facebook likes with PHP

In this tutorial I will show you how to get number of Facebook likes of any Facebook page or external link. For this I have make one function Facebook_like_count() that will retrieve Facebook like from Facebook database. In this function I have make query that will pull likes from Facebook database and return back to us in the pattern of JSON (JavaScript Object Notation). I have used Facebook API that will pass my query to Facebook and after this deliver response back to me. This type of script can be suitable if you are establishing social widget that display your social links. You can also check your Facebook page or external link page likes by using this script.



Source Code

count_facebook_like.php

 <?php  
 //php->php extensions->php_openssl  
 function facebook_like_count($url)  
 {  
      $sql = "SELECT like_count FROM link_stat WHERE url = '$url'";  
      $fURL = "https://api.facebook.com/method/fql.query?format=json&query=" . urlencode($sql);  
      // Facebook Response is in JSON  
   $response = file_get_contents($fURL);  
      return json_decode($response);  
 }  
 $fb = facebook_like_count('https://www.facebook.com/webslesson');  
 echo "Number of Facebook Likes - ". $fb[0]->like_count;  
 ?>  

2 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. nice blog very useful keep it up
    Explore world Knowledge in just 10 point
    visit Click Here

    ReplyDelete