Daniel Benzie
Daniel Benzie

Reputation: 479

pagination within the facebook API (photos)

I'm currently developing a facebook app that needs access to a users photos. It needs to loop through all of the users active photos however I'm having some trouble with the pagination aspect of the feed. I get results from the API like

stdClass Object
(
    [data] => Array
        (
            [0] => stdClass Object
                (
                    [id] => 10151796309135076
                    [from] => stdClass Object
                        (
                            [name] => Daniel Benzie
                            [id] => 762525075
                        )
                )
        )
)

obviosuly the above is an excerpt and then down the bottom there is a section for next and previous pages.

[previous] => https://graph.facebook.com/762525075/photos?access_token=xxxxxxx&limit=25&since=1338985293&__previous=1
[next] => https://graph.facebook.com/762525075/photos?access_token=xxxxx&limit=25&until=1332002972

this is always set- does anyone know the best way to loop through the photos in this case? thanks in advance (:

Upvotes: 2

Views: 2007

Answers (1)

phwd
phwd

Reputation: 19995

Keep calling the next url until there is no more data

while ($some_photos['data'])
{
    $all_photos = array_merge( $all_photos, $some_photos['data'] );

    $paging = $some_photos['paging'];
    $next = $paging['next'];

    $query = parse_url($next, PHP_URL_QUERY);
    parse_str($query, $par);  

    $some_photos = $facebook->api(
        $user_id."/photos", 'GET', array(
            'limit' => $par['limit'],
            'until'  => $par['until'] ));
}

Upvotes: 2

Related Questions