Facebook API – Using fql_query to get status and fan count from pages
Using the Facebook API to get the latest status updates and fan count for a facebook page.
Written by Daniel, January 15, 2010
Today I needed to get the 5 latest status updates from a Facebook page. A client had asked me if I could create a widget for wordpress that would show the latest blog posts, tweets and facebook updates in one widget.
While the latest blog posts and tweets were easy enough to set up, I had a few kinks figuring out how to get the data from Facebook, without requiring a session key (i.e. without logging in to Facebook).
This took a while to dig out, and I tried around 5 different approaches before figuring out that it was pretty simple actually. The Facebook API documentation is just terrible…
include('facebook-platform/php/facebook.php');
class fbPageStatus
{
public static function getStatusUpdates($fbkey, $fbsecret, $page_id)
{
$fb = new Facebook($fbkey, $fbsecret);
$fql = "SELECT message, updated_time FROM stream WHERE source_id = '" . $page_id . "'";
$fbupdates = $fb -> api_client -> fql_query($fql);
return $fbupdates;
}
}
// Usage:
var_dump(fbPageStatus::getStatusUpdates('7bc1ef16567735546bef620c32a23s76', 'ab3dfc4b7d8591f7ef2197ba754f16d1', '17975046895'));
Some simple FQL returns the result.
To get the number of fans, you would do:
$fql = "SELECT fan_count FROM page WHERE page_id = '" . $page_id . "'"; $fans = $fb -> api_client -> fql_query($fql);
Hope someone will find this usefull.
EDIT: All keys are fake, obviously. Replace with your own.
No Comments »
No comments yet.
Leave a comment