-1

I am creating a facebook app and i want to display the users albums with the cover image, i have tried with code #1 but it is really really slow as i make a facebook api call in the foreach which makes my app take forever, so I am looking for another way to make the api call just once and get all the info, so i tried with FQL multi query (code#2) but now i have a problem because if the album has no image it won´t return nothing, so please check my codes and if you have any idea how i can do this i will greatly appreciate it

code#1
$albums = $facebook->api('/me/albums?limit=0&fields=id,name,count,cover_photo');
foreach($albums['data'] as $album)
                {
                // get all photos for album
                $photos = $facebook->api("/{$album['id']}/?fields=picture,name,count");
                $foto = $photos['picture'];
                $nombre = $album['name'];

                $id = $album['id'];
                $count = $album['count'];
                $cover_photo = $album['cover_photo'];
                $nombre = $nombre." (".$count.")";


                echo "<li> <a href=\"album.php?numero_album=$id&nombre_album=$nombre \" title=\"$nombre\"><img src=\"https://graph.facebook.com/{$cover_photo}/\" alt=\"$nombre\" /> </a> </li>";
                }


code#2
$multiQuery = '{
  "albumes":"SELECT name,cover_pid,object_id,size FROM album WHERE owner=me()",
  "portadas":"SELECT src_big FROM photo WHERE pid IN (SELECT cover_pid FROM #albumes)"
        }';

        $param = array(       
     'method' => 'fql.multiquery',       
     'queries' => $multiQuery,       
     'callback' => '');       
        $queryresults = $facebook->api($param);

                foreach($queryresults[0]['fql_result_set'] as $album)
                {
                $nombre = $album['name'];
                $id = $album['object_id'];
                $count = $album['size'];
                $nombre = $nombre." (".$count.")";


                echo "<li> <a href=\"album.php?numero_album=$id&nombre_album=$nombre \" title=\"$nombre\"><img src=\"https://graph.facebook.com/{$cover_photo}/\" alt=\"$nombre\" /> </a> </li>";
                }
SecretAgentMan
  • 2,856
  • 7
  • 21
  • 41

1 Answers1

0

so i tried with FQL multi query (code#2) but now i have a problem because if the album has no image it won´t return nothing

Can’t really see your problem there. If I try your queries using the Graph API explorer, it gives me all my albums, and a cover photo for each one that has one. (So that’s 4 albums and 3 photos for me.)

If you’re having trouble to tell which photo is from which album – then just include the aid field in both queries.

it is really really slow as i make a facebook api call in the foreach which makes my app take forever

Of course it does, because each API call means another HTTP request, and those are slow.

If the FQL method doesn’t give you what you want, then make your API requests into batch requests – that reduces the number of HTTP requests, and therefor should speed things up quite well.

CBroe
  • 91,630
  • 14
  • 92
  • 150
  • Thanks for the reply! but how can i display the images of each album with the name of the album? sorry if this is a really stupid question but i am learning the Facebook API @CBroe – Jeronimo Cosio Jul 04 '12 at 19:08