This tutorial will help you to get Facebook Likes, Shares and Comments of any Facebook Page or External Link. I have make a function fb_count() that will fetch Facebook likes, shares and comments from Facebook Database. Under this function we make a query that will get likes, shares and comments from facebook database and response us in form of JSON (JavaScript Object Notation). We use Facebook API, that will pass our query to facebook and then give us response. This script can be useful if you are developing social widget that will show your social links, you can show your likes though this script.
[button-blue url=”http://demos.eggslab.net/facebook-likes-count-php/” target=”_blank” position=””]DEMO[/button-blue][button-green url=”http://demos.eggslab.net/downloads/8″ target=”_blank” position=””]DOWNLOAD[/button-green]
First we make a function that will pass one parameter and that parameter is our link.
function fb_count($url) { }
Then under this function first we make a query and ask it to SELECT share_count, like_count and comment_count from a table named link_stat. Obviously we need a KEY for for it so our Link or URL will be the KEY for this query.
$fql = "SELECT share_count, like_count, comment_count "; $fql .= " FROM link_stat WHERE url = '$url'";
Then we use Facebook API to pass above Query. After passing this query we will get our response in JSON.
$fqlURL = "https://api.facebook.com/method/fql.query?format=json&query=" . urlencode($fql);
When facebook response us in JSON we need to decode that JSON it can be through Buil-in function json_decode().
$response = file_get_contents($fqlURL); return json_decode($response);
Now it’s time to pass our Link or URL to get likes, shares and comments. Simply I use finction fb_count() and provide it a parameter as a Link or URL.
$fb = fb_count('http://www.9lessons.info/2015/03/audio-recording-with-custom-audio.html');
Wait a minute! There is nothing shown in page :'(
As I told you we get response in JSON so obviously we have to echo that response.
For facebook Shares Count
echo $fb[0]->share_count;
For Facebook Likes Count
echo $fb[0]->like_count;
For Facebook Comments Count
echo $fb[0]->comment_count;
That’s all!
[button-blue url=”http://demos.eggslab.net/facebook-likes-count-php/” target=”_blank” position=””]DEMO[/button-blue][button-green url=”http://demos.eggslab.net/downloads/8″ target=”_blank” position=””]DOWNLOAD[/button-green]
Leave a Reply