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
Hi.
Why your user name in Code is work but when I put my username or otherwise username is not work?
please help me !!!
this does not work for user profiles, only for pages. for getting the albums of a user profile, you would need a user access token, so it would be more complicated.
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’);
it should be like this: $contents = file_get_contents(‘http://graph.facebook.com/bladauhu/albums?fields=id,name,type&access_token=xxx’);
keep in mind that user tokens are valid for 2 hours only – unless you are using an extended one, see more info about that in my article about access tokens: http://www.devils-heaven.com/facebook-access-tokens/
facebook has update their privacy, not working now 🙁
what do you mean with “not working”? i just tested the calls, they still work fine.
Hi, I test it and it everything ok.
But when I test it on actual running website and after two days , it show error
file_get_contents(http://graph.facebook.com/v1.0/451510405966669/albums?limit=9): failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden
some say that my remote server block this connection.
It is indeed possible that your webserver blocks the connection to Facebook. You should also consider using Access Tokens in every call.
Thank you Andreas Teufel.
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
}
}
it does work, but facebook requires you to ALWAYS use an access token now.
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?
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 🙂
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…
you need to tell where exactly you got stuck, there´s example code for all those things in the facebook docs
”
There´s also an easy way to get the cover image of every album:
”
That is no longer true. You can have nested get elements and get rid of extra get requests:
https://graph.facebook.com/v3.2/page-id/albums?fields=id,count,description,name,updated_time,cover_photo.fields(link,id)
Thanx for the addition, of course declarative fields can be used too. They exist since many years already, but did not exist in 2010 when i wrote this article 🙂