如何显示我所有的 Facebook“喜欢”以及相关的“粉丝”总数?

发布于 2024-11-16 21:06:57 字数 702 浏览 0 评论 0原文

我一直在测试一种方法来显示我所有的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

飘落散花 2024-11-23 21:06:57

我不熟悉 Facebook API,但一般来说,当您进行 SQL 查询时,您不会希望过于频繁地查询数据库,因为访问数据库是一项昂贵的操作。

,而不是使用带有此查询的 for 循环一次又一次地选择每个页面 id 的粉丝数

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

因此,您可以从 for 循环中删除查询并重写它以一次选择所有粉丝数 ,在一个查询中,使用 IN SQL 语法,类似

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

// ...

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

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,

'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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文