Marian07
Marian07

Reputation: 2582

JSON - get facebook total count from url

This script takes the number of likes from a facebook fan page.

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
//Set Url of JSON data from the facebook graph api. make sure callback is set   with a '?' to overcome the cross domain problems with JSON
var url = "https://graph.facebook.com/tenniswarehouse?callback=?";

//Use jQuery getJSON method to fetch the data from the url and then create our unordered list with the relevant data.
$.getJSON(url,function(json){
    var html = "<ul><li>" + json.likes + "</li></ul>";
    //A little animation once fetched
    $('.facebookfeed').animate({opacity:0}, 500, function(){
        $('.facebookfeed').html(html);
    });
    $('.facebookfeed').animate({opacity:1}, 500);
});
});
</script>
</head>
<body>
<div class="facebookfeed">
<h2>Loading...</h2>
</div>
</body>
</html> 

Source

I tried to make him take the total number of likes,shares and comments from a url("total_count") , but it didn't work.

<script type="text/javascript">
$(function() {
//Set Url of JSON data from the facebook graph api. make sure callback is set with a '?' to overcome the cross domain problems with JSON
var url = "https://graph.facebook.com/fql?q=SELECT%20url,%20normalized_url,%20share_count,%20like_count,%20comment_count,%20total_count,commentsbox_count,%20comments_fbid,%20click_count%20FROM%20link_stat%20WHERE%20url=    %27http://www.google.com%27?callback=?";

//Use jQuery getJSON method to fetch the data from the url and then create our unordered list with the relevant data.
$.getJSON(url,function(json){
var html = "<ul><li>" + json.total_count + "</li></ul>";
//A little animation once fetched
$('.facebookfeed').animate({opacity:0}, 500, function(){
$('.facebookfeed').html(html);
});
$('.facebookfeed').animate({opacity:1}, 500);
});
});
</script>
</head>
<body>
<div class="facebookfeed">
<h2>Loading...</h2>
</div>
</body>
</html>

Could you please help me make the script get the "total_count" from links?

Upvotes: 0

Views: 3544

Answers (1)

Tobi
Tobi

Reputation: 31479

I guess

var html = "<ul><li>" + json.data[0].total_count + "</li></ul>";

should do... Be sure to remove surplus spaces from the URL in the FQL as well.

Have a look at this Fiddle: http://jsfiddle.net/e24ey/1/

Upvotes: 2

Related Questions