-1

我正在创建一个 facebook 应用程序,我想显示带有封面图像的用户相册,我已经尝试使用代码 #1,但它真的很慢,因为我在 foreach 中进行了 facebook api 调用,这使得我的应用程序永远需要,所以我正在寻找另一种方法来进行一次 api 调用并获取所有信息,所以我尝试使用 FQL 多查询(代码#2)但现在我遇到了一个问题,因为如果专辑没有图像,它不会返回任何内容,所以请检查我的代码,如果您知道我该怎么做,我将不胜感激

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>";
                }
4

1 回答 1

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.

于 2012-07-04T10:11:04.310 回答