I looked into this last year, and published a blog article showing how to do it. It was fine as far as it went, but I want to expand it. My goal is to write a tool allowing me to bulk upload pictures into a gallery of a business page I administer. So that, for instance, whenever new stock arrives, I can generate a gallery containing images of the stock and links to buy it onsite.
So far, I have had partial success. I can create a gallery, however, it doesn’t generate the crucial wall post that Facebook itself does if you create a gallery using Facebook. You know, it looks something like this:

You get a lovely big wallpost, featuring the cover photo and smaller thumbs of some of the other photos. Without this wall post, nobody will really be aware that you have added a gallery, so we need this wall post.
The bad news is that I have yet to fathom out how to get Facebook to make such a post for us. However, I have got around this myself by first creating the gallery using facebook, and adding four pictures. This will generate a gallery as shown. I have then used the automated process I am about to describe, to stuff the gallery full of all the other pictures I wanted to add. So you get the nice wall post, but you don’t have to spend hours manually adding all the pictures.
It is a compromise, but its a heck of a lot better than having to manually add each picture.
So, how do we add pictures automatically, to a gallery we have created in Facebook? That is what I am about to explain…
First things first, I am assuming the following: the pictures you want uploaded to the gallery will be located on the same website that you are going to write the uploader. I am also assuming you have a suitable app id etc.
When you connect your uploader app to facebook, you will need the following permissions:
read_stream, manage_pages, publish_stream, photo_upload, user_photos, user_photo_video_tags
Ok, so what next? Well, lets assume you have the following:
$user = $facebook->getUser();
$accessToken = $facebook->getAccessToken();
To obtain a list of fanpages associated with your id, you need to make the following call:
$fanpages = $facebook->api('me/accounts?access_token='.$accessToken);
Please note the following: the data returned from facebook queries is stored as records in a multi-dimensional array. Normally, all the data is wrapped in an array called “data”. So when you are processing a facebook array, you should use:
Foreach ($fanPages[data] as $fa_fanpage)
Instead of:
Foreach ($fanPages as $fa_fanpage)
Which would just return one array containing everything.
When we give the structure of a facebook result set, we will omit the data array, and assume you are accessing it as above. This is for the sake of brevity. So the structure given will refer to a single record within the whole result set.
Each record will consist of one or more fields. Occasionally, those fields will themselves contain arrays. In this case, we give the full structure of the array, as some possess their own data wrapper, but by no means all of them.
So, to get the fanpages of your account, you make the call given above, and this will return an array of results with the following structure:
- name
- access_token
- category
- id
- perms=>array(LIST OF PERMS)
This gives you a list of fanpages. Assuming you have selected the fan page you want, at the very least you should assign a couple of variables:
- $fanPageId = id
- $fanPageAccessToken = access_token
Now, to find out which galleries belong to your fan page you have to do the following:
$fan_albums = $facebook->api($fanPageId.'/albums/');
Note that you are not querying your own id, but the id of the fanpage.
Each fan_album will have the following structure:
- id
- from – array(name, category, id)
- name
- description
- link
- cover_photo
- privacy
- count
- type
- created_time
- updated_time
- can_upload
- likes => array => [data]=>[n][id][name]
- paging => array => next
The key field there is once again, id, so store that in $albumId;
So, now you have your $fanPageId, your $fanPageAccessToken, and your $albumId. You can now add a picture to your album using the following call:
$newphotodata = array(
'access_token' => $fanPageAccessToken,
'message' => $message,
'no_story' => 1,
'aid' => $albumId,
'image' => '@' . $picturePath);
$uploadedphoto = $facebook->api('/' . $albumId . '/photos/', 'post', $newphotodata);
The important things to note are:
- When you make the call to the facebook api, you are using the access token of the fanpage, not your user access token.
- picturePath must point to the picture on the server
- It takes time to upload a picture. So if you are attempting to upload 100s of pictures, you will probably find your PHP file timing out. The way around this would be to keep the routine that does the actual uploading in a separate PHP file, and use Ajax to pump data tot it, in groups of 10.
We have used the no_story flag. If you don’t include this, then you get a story posted for EVERY picture loaded. However, before you get too excited, the story posted is NOT the same as the one Facebook generates when you create a gallery, instead, you get one story for EACH picture, which is way too many.
I haven’t finished with photo galleries by a long shot. I have posted my documentation thus far as it may be of use to somebody, and it will certainly be of use to me when I return to it…