At the moment, you're making two separate calls to Facebook's database which is slowing everything down. Facebook offer their multiquery so that you can do everything in as few DB calls as possible. So the calls you should think about using are:
"query1":"SELECT page_id, name, fan_count FROM page WHERE page_id IN ($pagesIds)"
And because they allow you to reference a prior query, you can just include it after a #:
"query2":"SELECT message FROM stream WHERE source_id IN (SELECT page_id FROM #pages) LIMIT 2"
The PHP you need to use is something like this:
$query = array(
"pages"=>"SELECT page_id, name, fan_count FROM page WHERE page_id IN ($pagesIds)",
"messages"=>"SELECT message FROM stream WHERE source_id IN (SELECT page_id FROM #pages) LIMIT 2"
);
$fql_url = $facebook->api(array(
'method' => 'fql.multiquery',
'queries' => $query
));
print_r($fql_url);
If the second query isn't going through, try testing the FB DB with just this query and see if it works. If the query doesn't return anything by itself, the problem might be with permissions (i.e. accessing a sensitive table -- but I don't think this is the case). Another problem which I've frequently encountered is how FQL trips itself up with whitespace, so try omitting all possible whitespace from your array:
$query = array("pages"=>"SELECT page_id, name, fan_count FROM page WHERE page_id IN ($pagesIds)","messages"=>"SELECT message FROM stream WHERE source_id IN (SELECT page_id FROM #pages) LIMIT 2");
Wow, that's readable...
This was taken from Facebook's documentation on FQL, though, so you might need to adapt it for your web application if using a third party library. All of your data is saved in $fql_url
. All you need to do is loop through it and echo out the information you want. If you want to see a summary of everything it contains, think about using print_r()
or var_dump()
just to get your bearings.
EDIT
The reason that you're receiving an empty array for the second query is because you don't seem to have permissions for the stream
table. If you check Facebook's documentation, they mention the criteria needed to access this table:
To read the stream table you need
read_stream
permissions for all posts that the current session user is
able to view
read_insights
permissions to see post impressions for any
posts made by a Page owned by the current session user
To check what permissions you have, you can run this query:
$check_query = $facebook->api(array(
"method" => "fql.query",
"query" => "SELECT * FROM permissions WHERE uid=me()"
));
foreach($check_query[0] as $k => $v) {
if($v === "1") {
echo "<strong>$k</strong> permission is granted.<br>";
} else {
echo "<strong>$k</strong> permission is not granted.<br>";
}
}