简体   繁体   中英

Facebook Friends Count?

What is the best way to count a Facebook user's friends...

I'm currently using (PHP):

$data = $facebook->api('/me/friends');
$friends_count = count($data['data']);

and its very slow... (about 2 secs)

Querying the facebook api sends a request to facebook. Because its a common http-request this Probably takes most of the time. There is usually no way around it. If you need the values more often, you should cache them somewhere

if (file_exists($cacheFile)) {
  $data = file_get_contents($cachefile);
} else {
  $data = $facebook->api('/me/friends');
  file_put_contents($cacheFile, $data);
}
$friends_count = count($data['data']);

Remember to update the cache file from time to time.

If you are not processing the data given by Facebook on server side, instead of doing it using PHP, you can use JavaScript graph API to fetch, it can fetch it using ajax which wont effect your page load time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM