Saturday, July 19, 2014

Get Facebook Likes Count of a page using Graph API

<?php
echo facebooklike('209414452444193');

function facebooklike($page_id){

$likes = 0; //Initialize the count
//Construct a Facebook URL
$json_url ='https://graph.facebook.com/'.$page_id.'';
$json = get_contents($json_url);
$json_output = json_decode($json);

//Extract the likes count from the JSON object
if($json_output->likes){
$likes = $json_output->likes;
}
   return $likes;
 
}

function get_contents($url)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}

?>

1 comment:

  1. Hey, great blog, but I don’t understand how to add your site in my rss reader. Can you Help me please?
    Buy Facebook Likes Cheap

    ReplyDelete