开发者

How to show all my facebook 'likes' and the associated total of 'fans'?

开发者 https://www.devze.com 2023-03-14 15:38 出处:网络
I have been testing a way to show all my facebook \'likes\' and the associated amount of \'fans\'. The code below works but is very...very...very slow, and i assume it is so slow because of the large

I have been testing a way to show all my facebook 'likes' and the associated amount of 'fans'. The code below works but is very...very...very slow, and i assume it is so slow because of the large number of queries that have to go through Facebook's api due to the foreach function. Is there a away to do the same thing as i do below, but with only one query (not using the foreach)?

require_once('src/facebook.php');
$facebook = new Facebook(array(
    'appId'  => '12345',
    'secret' => 'blablabla',
    开发者_C百科'api' => '172737262',
    'cookie' => true,
));
$likes = $facebook->api('/me/likes');

foreach($likes[data] as $value){
$id = $value['id'];
$fans = $facebook->api(array(
    'method' => 'fql.query',
    'query' => "select fan_count,name from page where page_id = $id;"
));

echo "$id - {$fans[0]['fan_count']} - {$value['name']}<br>";
unset($id,$fans);
}


I'm not familiar with the Facebook APIs, but in general, when you're doing SQL queries, you don't want to query the database more often than you have to, because accessing the database is an expensive operation.

So instead of using a for loop with this query to select the fan count for each page id one at a time, over and over again,

'query' => "select fan_count,name from page where page_id = $id;"

you could possibly remove the query from the for loop and rewrite it to select all the fan counts at once, in one query, using the IN SQL syntax, something like

$ids = "id1, id2, ... , idn";

// ...

'query' => "select fan_count,name from page where page_id in ($ids);"

Maybe that might work?

0

精彩评论

暂无评论...
验证码 换一张
取 消