Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NicholasRBowers/4120967 to your computer and use it in GitHub Desktop.
Save NicholasRBowers/4120967 to your computer and use it in GitHub Desktop.
Extract comments from your page on Facebook
<?php
// Displays comments for a certain URL.
$url = 'http://developers.facebook.com/docs/reference/fql/comment/';
// FQL multi-query to fetch all the data we need to display comments.
$queries = array(
'q1' => 'SELECT post_fbid, fromid, object_id, text, time FROM comment WHERE object_id IN (SELECT comments_fbid FROM link_stat WHERE url ="'.$url.'")',
'q2' => 'SELECT post_fbid, fromid, object_id, text, time FROM comment WHERE object_id IN (SELECT post_fbid FROM #q1)',
'q3' => 'SELECT name, id, url, pic_square FROM profile WHERE id IN (SELECT fromid FROM #q1) or id in (SELECT fromid FROM #q2)',
);
// Note format json-strings is necessary because 32-bit PHP sucks at decoding 64-bit ints :(
$result = json_decode(file_get_contents('http://api.facebook.com/restserver.php?format=json-strings&method=fql.multiquery&queries='.urlencode(json_encode($queries))));
$comments = $result[0]->fql_result_set;
$replies = $result[1]->fql_result_set;
$profiles = $result[2]->fql_result_set;
$profiles_by_id = array();
foreach ($profiles as $profile) {
$profiles_by_id[$profile->id] = $profile;
}
$replies_by_target = array();
foreach ($replies as $reply) {
$replies_by_target[$reply->object_id][] = $reply;
}
/**
* print a comment and author, given a comment passed in an an array of all profiles.
* @param object $comment as returned by q1 or q2 of the above FQL queries
* @param array $profiles_by_id, a list of profiles returned by q3, keyed by profile id
* @returns string markup
*/
function pr_comment($comment, $profiles_by_id) {
$profile = $profiles_by_id[$comment->fromid];
$author_markup = '';
if ($profile) {
$author_markup =
'<span class="profile">'.
'<img src="'.$profile->pic_square.'" align=left />'.
'<a href="'.$profile->url.'" target="_blank">'.$profile->name.'</a>'.
'</span>';
}
return
$author_markup.
' ('.date('r', $comment->time).')'.
': '.
$comment->text;
}
print '<html><body>';
// print each comment
foreach ($comments as $comment) {
print
'<div style="overflow:hidden; margin: 5px;">'.
pr_comment($comment, $profiles_by_id).
'</div>';
// print each reply
if (!empty($replies_by_target[$comment->post_fbid])) {
foreach ($replies_by_target[$comment->post_fbid] as $reply) {
print
'<div style="overflow:hidden; margin: 5px 5px 5px 50px">'.
pr_comment($reply, $profiles_by_id).
'</div>';
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment