views:

85

answers:

2

Hey guys On my profile page, I'm trying to pull all my likes from facebook and display the detail about each like. For example, if I like the tv show Seinfeld, I will display the Seinfeld logo along with how many like it etc.

I'm using the php sdk and it takes forever to pull the data.

Currently I have only 24 likes and it takes 75 seconds to pull this data.

This is the code I am using

<pre>
$likes = $facebook->api('/me/likes');

foreach($likes['data'] as $like) {

$like_item = $facebook->api($like['id']);
?>
<fb:profile-pic uid="&lt;?php echo $like_item['id'];?>" size="square"></fb:profile-pic> 
<?php 
echo $like_item['name'];
?>
<fb:like href="<?php echo $like_item['link'];?>"></fb:like> 
<?
}

</pre>

Any idea why its taking so long. Am I doin it the right way or is there a better way to approach this. Thanks a bunch

+2  A: 

Yeah, there is a much better approach than this! Basically, you're making an additional API call for EACH like. If you like 75 things, you're making 76 API calls, each of which could take a second. Instead of iterating over ‘$likes‘, do:

$likes_csv = implode(',',$likes['data']);
$likes_items = $facebook->API('/?ids='.$likes_csv);

Then you can do what you want with ‘$likes_items‘

Mike Sherov
Thanks Mike - that makes sense - in the example you have shown - we wont be able to use the implode function here right as $likes['data'] would be array of arrays. But I get the picture - from $likes[data] - get all the like_ids and then make one api call - thanks a bunch - let me just try that out and close this one - thanks for ur help
Gublooo
Additionally, don't build the query string using string concat. Instead do something like `$facebook->api('/', array('ids' => $ids))` -- this will ensure it doesn't hit the URL length limit as the data will be sent using a POST.
daaku
Thanks for your inputs - but as Yuliy points out, I am able to pull the likes along with the likes details in one pass itself.
Gublooo
+2  A: 

You should be able to do $facebook->api('/me/likes?fields=id,name,link') to fetch all of the needed data in one pass.

Yuliy
Thanks this worked out great
Gublooo

related questions