3

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!

1
  • This question also titled: "Things Facebook probably doesn't want you to do..."
    – hackartist
    Feb 20, 2012 at 20:43

2 Answers 2

8
  $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);
8
  • This is probably it but I don't know where to put it in my code. :/ I think i might be a little in over my head. Feb 20, 2012 at 20:03
  • what exactly you want to achieve? get image urls or you need to group them by albums? Feb 20, 2012 at 20:05
  • I want to to get photos(image urls) clustered by albums. I tried putting the code you suggested where I thought it went in the code I posted with the question but I didn't work. Feb 20, 2012 at 20:08
  • I edited the sample. Place it below print_r($albums) and then check what print_r($pictures) is holding Feb 20, 2012 at 20:14
  • Okay, I did that, but it is only printing $albums, after that it doesn't print pictures. I echoed two line breaks before the print $pictures and after that it's blank. my testing page: tiffanyisrael.com/profiles.php even if it doesn't work thanks for you help so far! Feb 20, 2012 at 20:22
0

Why can't you loop over the $albums array?

pseudocode:

foreach $album in $albums
{
  $list_pictures += $facebook->api(...);
}
2
  • Well, I'm a newb' to start. :) So I put the code in that you suggested after $albums = $facebook->... and I get a syntax error in Dreamweaver. I moved it around in the code and I still doesn't work. When I test it on the server it shows a blank screen. :/ Feb 20, 2012 at 19:46
  • @OUHSDWebmaster it was just pseudo-code, meaning, it wasn't actually supposed to be grammatically correct. See here php.net/manual/en/control-structures.foreach.php for more info and to LEARN :) Feb 20, 2012 at 19:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.