Search code examples
phpfacebookfacebook-graph-apiphotos

Get all photos from all albums


I've got this code to get photos from a specific album, however I want to ask for all albums, get their id's and for each id list all the pictures.

<?php 
 require 'facebook/src/facebook.php';
  $appId = '[app_id]';
  $secret = '[app_secret]';
  $access_token = '[access_token]';
  $facebook = new Facebook(array('appId' => $appId, 'secret' => $secret, ));
  $facebook->setAccessToken($access_token);



$albums = $facebook->api('/me/albums?fields=name,count'); 
print_r ($albums);


$album_ID = "309608592385363"; //album ID
$list_pictures = $facebook->api('/'.$album_ID.'/photos', array('access_token' => $access_token));
print_r ($list_pictures);



?>

Bare in mind I removed variable like my app id, etc. They do work each on their own accord, by printing $albums and $list_pictures but I'm trying to bridge the gap between them so that albums tells list pictures each album_id to access. I'm doing this for a client who isn't going to getting into the hand coding.

THANKS!


Solution

  •   $albums = $facebook->api('/me/albums?fields=id'); 
      $pictures = array();
      foreach ($albums['data'] as $album) {
        $pics = $facebook->api('/'.$album['id'].'/photos?fields=source,picture');
        $pictures[$album['id']] = $pics['data'];
      }
    
      //display the pictures url
      foreach ($pictures as $album) {
        //Inside each album
        foreach ($album as $image) {
          $output .= $image['source'] . '<br />';
        }
      }
      exit($output);