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:
[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'] . '">';
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:
The image can be used as centered background image, for example:
background-position: center center;
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']['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.
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:
Check out my article about Access Tokens for more information.
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:
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:
Formatting is up to you, this is just how to get the data 🙂