我想从facebook php-sdk中提取我的朋友列表“喜欢”某些仅来自某个app_id的帖子。让我举个例子,如果你去www.rottentomatoes.com你可以在朋友/朋友部分看到所有的朋友在任何电影上写评论/评论。
我认为FQL是一种干净利落的方式,但事实并非如此,因为处理数据的时间过长,我认为它还没有为在线应用做好准备。
这是我到目前为止提出的代码:
function cmp($a, $b)
{
if ($a["like"] == $b["like"]) {
return 0;
}
return ($a["like"] > $b["like"]) ? -1 : 1;
}
try
{
$fql = 'SELECT uid FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1='.$uid.') AND has_added_app = 1';
$_friends = $facebook->api(array('method' => 'fql.query','query' => $fql));
}
catch(FacebookApiException $e)
{
print_r($e);
error_log($e->getType());
}
// Extract the user ID's returned in the FQL request into a new array.
$likes = array();
if (is_array($_friends) && count($_friends) > 0)
{
foreach ($_friends as $friend)
{
$fid = $friend["uid"];
try
{
$fql = 'SELECT url FROM url_like WHERE user_id = '.$fid.' AND strpos(lower(url),"muchogusto") >=0';
$_likes = $facebook->api(array('method' => 'fql.query','query' => $fql));
$likes[] = array("uid" => $fid, "like" => count($_likes));
}
catch(FacebookApiException $e)
{
$likes[] = array("uid" => $fid, "like" => 0);
continue;
error_log($e->getType());
}
}
}
usort($likes, "cmp");
print_r($likes);
我有很多问题,当然我可以帮助我,因为我是新手。
有人可以帮助我在没有FQL的情况下编码吗?我只需要这个:
非常感谢你的帮助。
答案 0 :(得分:1)
FQL不是问题。这是你在foreach
循环中进行FQL调用的事实。这就是减慢程序速度的原因。
如果你在另一个foreach
循环中调用此函数,那么就忘了它。
尝试使用FQL多重查询进行一次调用。
$queries['my_friends'] =
'SELECT uid FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1=me())
AND has_added_app = 1';
$queries['liked_url'] =
'SELECT url,user_id FROM url_like WHERE user_id IN (SELECT uid FROM #my_friends)
AND strpos(lower(url),"muchogusto") >=0';
$multiquery = json_encode($queries);
$result = $facebook->api(array('method' => 'fql.multiquery','queries' => $multiquery));