I have been looking around for a jQuery json method to pull in stats for a given facebook, for a project that I am starting in uni. I found this had already been asked here: trying to pull the number of likes from a facebook fanpage using JSONP and JQuery
Having used the code for my page, it works great, and is lightweight, however I am now wanting to use this code to pull in the results from multiple pages, but have tried to find a solution and hit a brick wall.
My current code, pulling in one page’s data is:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/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/immbudden?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><li>" + json.about + "</li></ul>";
//A little animation once fetched
$('.facebookfeed').animate({opacity:0}, 500, function(){
$('.facebookfeed').html(html);
});
$('.facebookfeed').animate({opacity:1}, 500);
});
});
</script>
<link rel="stylesheet" href="style.css" media="all">
</head>
<body>
<div id="wrapper"><!--wrapper open-->
<div class="facebookfeed">
<h2>Loading...</h2>
</div>
</div><!--wrapper closed-->
</body>
I am literally just starting to properly delve into jQuery, so any help at all is greatly appreciated!
Thanks in advance, Michael
You can batch request the Facebook API so you don’t even need to make multiple calls for multiple pages 🙂
The batch request looks something like this:
https://graph.facebook.com/?ids=id1,id2,id3etc. You would then need to loop through the results to print them out. So the entire change would look like so:I’ve created a working jsFiddle for you here: http://jsfiddle.net/rYyzf/