如何显示我所有的 Facebook“喜欢”以及相关的“粉丝”总数?
我一直在测试一种方法来显示我所有的 Facebook“喜欢”和相关的“粉丝”数量。下面的代码可以工作,但是非常...非常...非常慢,我认为它之所以如此慢,是因为由于 foreach 函数,大量查询必须通过 Facebook 的 api。有没有一种方法可以做与我下面相同的事情,但只有一个查询(不使用 foreach)?
require_once('src/facebook.php');
$facebook = new Facebook(array(
'appId' => '12345',
'secret' => 'blablabla',
'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 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',
'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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不熟悉 Facebook API,但一般来说,当您进行 SQL 查询时,您不会希望过于频繁地查询数据库,因为访问数据库是一项昂贵的操作。
,而不是使用带有此查询的 for 循环一次又一次地选择每个页面 id 的粉丝数
因此,您可以从 for 循环中删除查询并重写它以一次选择所有粉丝数 ,在一个查询中,使用 IN SQL 语法,类似
Maybe that might work?
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,
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
Maybe that might work?