Facebook API – Albums Of Facebook Pages

In this article, i will show you how to get Albums/Photos with the Graph API. For User Albums you will need an Access Token, for Pages it´s pretty easy and you don´t even need an App.

The typical access to the Graph API always follows the same scheme:

https://graph.facebook.com/[ID]/[Connection-Type]

[ID] can be the id of a User profile, the id of a Page, Event, Group, Application, Album, Photo, Video, … but you can also use the Vanity URL instead.

[Connection-Type] can be “Albums” for a Facebook Page, to get the Albums of that Page:

Get Albums (albums.php)

$contents = file_get_contents('http://graph.facebook.com/bladauhu/albums?fields=id,name,type');
$albums = json_decode($contents,true);
$albums = $albums['data'];

foreach ($albums as $row)
{
	if ($row['type'] == 'normal')
	{
		echo '<a href="photos.php?albumid=' . $row['id'] . '">';
		echo $row['name'];
		echo '</a><br>';
	}
}

If you only need specific fields, it´s good to include those in the call to the Graph API, so it takes less time. In this case, i include the id, the name and the type of the Albums. The type can be one of the following:

  • profile: Profile Photo
  • wall: Wall Photo
  • normal: Custom Album

In the example above, we only show custom Albums. file_get_contents gets you a JSON string, you can convert that string to an associative Array with json_decode (PHP >= 5.2.0!). For testing, you can just put the Graph-Link in the address field of your browser and get the data as JSON.

The for loop displays a link to every Album, sorted descending by the date of creation. There´s also an easy way to get the cover image of every album:

http://graph.facebook.com/[Album-ID]/picture

The image can be used as centered background image, for example:

background-position: center center;
background-repeat: no-repeat;

Get Photos (photos.php)

Again with just a few lines, you can get the Photos of a specific Album:

$albumid = $_GET['albumid'];
$contents = file_get_contents("http://graph.facebook.com/$albumid/photos?limit=30");
$photos = json_decode($contents,true);
$photos = $photos['data'];

foreach ($photos as $row)
{
	echo '<img src="' . $row['images'][1]['source'] . '" /><br>';
}

The “images” Array includes 4 versions of every Photo, each one with the parameters “height”, “width” and “source” (= direct link to Photo).

Different Photos in an Album either have to be resized with PHP (or whatever server language you prefer) or you take one of the 4 versions as background image like in the example before. Include a Lightbox to the whole story and BAM, you got yourself a nice gallery.

Access Token

I did not mention this before, but you should ALWAYS use an Access Token for your API calls. An App Access Token would be good enough for the example, it is valid forever and easy to create:

http://graph.facebook.com/$albumid/photos?limit=30&access_token=APP-ID|APP-SECRET

Check out my article about Access Tokens for more information.

Limit/Paging

The number of returned elements per Graph API call is 25 per default, you can change this with the “limit” parameter. For paging, there is the parameter “offset”. You don´t have to create the Graph API calls for the next batch of Photos by yourself, there will be two Links for this in the returned JSON-Object:

"paging": {
    "next": "http://graph.facebook.com/388843868683/photos?limit=30&offset=60",
    "previous": "http://graph.facebook.com/388843868683/photos?limit=30&offset=0"
}

If you want to pimp your Facebook gallery by showing all the comments of your Photos, there´s another easy call to the Graph API for you:

http://graph.facebook.com/[Photo-ID]/comments

Formatting is up to you, this is just how to get the data 🙂

Links

  1. Facebook Graph API

17 thoughts on “Facebook API – Albums Of Facebook Pages”

  1. Hi.
    Why your user name in Code is work but when I put my username or otherwise username is not work?
    please help me !!!

  2. Yes I got it myself.
    Just where I put access token in this script :
    $contents = file_get_contents(‘http://graph.facebook.com/bladauhu/albums?fields=id,name,type’);

  3. I think this script does not work anymore. Get the following message:
    {
    “error”: {
    “message”: “An access token is required to request this resource.”,
    “type”: “OAuthException”,
    “code”: 104
    }
    }

      1. so which access token should we use now. In my scope there is no facebook user, I just want to keep my website sync with facebook albums in gallery of the website so my page editor will post the data on my fb page and my website displays it to the users?

        1. in order to do something with a facebook page, you have to use a page token – and page tokens are generated with a user token. afaik this did not change in the last years. i hope that is what you wanted to know 🙂

  4. Great writeup! I’m curious if you would know the easiest way to pull just the photo information?

    Example of what I’m trying to do:
    albums.php fetches all albums.
    album.php fetches all pictures in an album.
    image.php shows the details (and the individual) picture…

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.