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)

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:

Get Photos (photos.php)

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

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:

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

15 thoughts on “Facebook API – Albums Of Facebook Pages

  1. Lord Milad says:

    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. Lord Milad says:

    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. Ed says:

    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
    }
    }

  4. James says:

    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 Reply

Your email address will not be published.

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