facebook api is very slow
i'm trying to make an application, getting all the birthdays and other information from you facebook friends, using the facebook php sdk.
i get the friendlist and with a foreach loop, get all the info for every friends' id.
This all works, only it takes 4 min+ to get all the information. (for every friend i have to make a new call)
I can't expect users of my application to wait that long.
Is there a way to get the info faster.
this is my code:
//$arrFriends is a list with all id's and names of friends
foreach ($arrFriends as $f => $value)
{
echo('');
$fql = "select uid, email, name, hometown_location, sex, birthday, education, relationship_status, pic_square from user where uid=" . $value[id];
$param = array('method' => 'fql.query','query' => $fql,'callback' => '');
开发者_如何学C $friend = $facebook->api($param);
//dan opslaan in de database:
print_r($friend);
echo('
');
}
You can use
SELECT uid, name, pic_square FROM user WHERE uid = me()
OR uid IN (SELECT uid2 FROM friend WHERE uid1 = me())
Reference: https://developers.facebook.com/docs/reference/fql/
Same result, fast, quick, elegant solution. May the performance be with you.
Another solution (to be an idea for you)
If you have hundreds of friends, well that's pretty normal and expected. You're requesting Facebook API for hundres of times in the loop.
Many scalable facebook applications do that job asynchronously in the background (in a task queue) or maybe completely on a different machine to not to slow down and consume CPU bursts.
Try to use OR
conjuctives in your FQL queries.
$fql = "select [...] from user where uid=123213312 OR uid=129038823 OR uid=893423890
But in this case your FQL query gets larger and larger so that you have to be divide it. Therefore use first solution.
Each fql query involves an http request to facebook, which is likely to be slow.
If you can work out a query to produce all of the results in a single response, and then process that response in a loop, you are likely to get much better performance.
If you don't want to use FQL, batch requests are designed for exactly this purpose. You build an array of graph requests and send them together. According to the documentation they're processed in parallel.
精彩评论