Facebook Fans on WordPress page

If you used this snippet to show Facebook Fans on your webpage:

[code language=”php”]
$page_id = “YOUR PAGE-ID”;
$xml = @simplexml_load_file(“http://api.facebook.com/restserver.php?method=facebook.fql.query&query=SELECT%20fan_count%20FROM%20page%20WHERE%20page_id=”.$page_id.””) or die (“a lot”);
$fans = $xml->page->fan_count;
echo $fans;
[/code]

You should realize that it is not working anymore and show error:

[code language=”xml”]

12

REST API is deprecated for versions v2.1 and higher (12)



method
facebook.fql.query


query

SELECT fan_count FROM page WHERE page_id=YOUR FACEBOOK PAGE ID




[/code]

This is because facebook REST API is not longer available and you should move forward.
We suggest to use facebook Graph Api v2.7. (latest version at post writing time)

Add code below to your functions.php file:

[code language=”php”]
function fbLikeCount($id){
//Construct a Facebook URL
$appid=’YOUR FACEBOOK APP ID’;
$appsecret=’YOUR FACEBOOK APP SECRET’;
$json_url =’https://graph.facebook.com/’.$id.’?access_token=’.$appid.’|’.$appsecret.’&fields=fan_count’;
$json = file_get_contents($json_url);
$json_output = json_decode($json);

//Extract the likes count from the JSON object
if($json_output->fan_count){
return $likes = $json_output->fan_count;
}else{
return 0;
}
}
[/code]

If you do not know how to get Facebook App ID and Secret please read this post.
In order to show Facebook Fans count you should use this code on your page:

[code language=”php”]
echo fbLikeCount(‘bernusports.lv’); //Your Facebook page alias or Facebook page ID.
[/code]

The result you can see here.

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

E-mail: info@itower.lv
2023 © Copyright - iTower.lv