Claire
Claire

Reputation: 43

Get fan count for lots of facebook pages

I have lots of facebook pages (we're a club) and would like to display them in order of their fan count. I'm new to php and know nothing about API. All the code I can find to return the fan count refers to apps, and needs their IDs and secrets, which I don't have with a business page.

I have this:

http://api.facebook.com/method/fql.query?format=json&query=select+fan_count+from+page+where+page_id%3D355692061120689

When I put this into a browser it gives me what I need. How do I write this in php so it gives me a variable to work with?

Thanks.

Upvotes: 2

Views: 2588

Answers (1)

Daedalus
Daedalus

Reputation: 7722

This data is called a JSON string. There is a function in php called json_decode(). Using this in tandem with file_get_contents(), we can retrieve the value, and echo it in php:

<?php
$json = file_get_contents('http://api.facebook.com/method/fql.query?format=json&query=select+fan_count+from+page+where+page_id%3D355692061120689');

$decode = json_decode($json);

echo $decode[0]->fan_count;
?>

To be clear, $decode is an array, whose first value is a php object, whose variable fan_count holds the data.

In regards to the graph url, just change..

echo $decode[0]->fan_count;

to..

echo $decode->likes;

of course assuming you've changed the file_get_contents() url to that of your graph url.

Upvotes: 3

Related Questions