Facebook Cookbook - Jay Goldman [154]
Problem
I need to retrieve specific photos, either because they contain a specific user, are in a specific album, or I have their pids (photo IDs).
Solution
Use the Photos.get() method. To retrieve photos of a specific user, call:
$photos = $facebook->api_client->photos_get('561415460',null,null);
To retrieve photos from a specific album, get the aid using Photos.getAlbums() and then pass it as the middle parameter (note that the aid shown in the URL when you’re looking at a specific album isn’t the album’s real aid and won’t retrieve the photos here):
$photos = $facebook->api_client->photos_get(null,'1234567890123456789',null);
To retrieve specific photos, pass an array of aids as the final parameter:
$targetPhotos = array('1234567890123456789','9876543210987654321');
$photos = $facebook->api_client->photos_get(null,null,$targetPhotos);
You can combine the three parameters in any way you’d like, allowing you to filter for photos from specific albums that have been tagged with a specific user, or the subset of a specific set of photos that have been tagged with a specific user, etc.
Discussion
This method will return a multidimensional array of photo elements, each containing the following:
[0] => Array
(
[pid] => 1234567890123456789
[aid] => 9876543210987654321
[owner] => 1345
[src] => http://photos-f.ak.facebook.com/photos-ak-sf2p/
v283/16/97/12345/s....jpg
[src_big] => http://photos-f.ak.facebook.com/photos-ak-
sf2p/v283/16/97/561415460/n....jpg
[src_small] => http://photos-f.ak.facebook.com/
photos-ak-sf2p/v283/16/97/561415460/t....jpg
[link] => http://www.facebook.com/photo.php?pid=1122334&id=12345
[caption] =>
[created] => 1212879873
)
FQL equivalent
If you’d prefer to use FQL to access photos, the equivalent query is:
SELECT pid, aid, owner, src FROM photo WHERE pid IN (SELECT pid FROM
photo_tag WHERE subject=$subj_id) AND aid=$aid AND pid IN ($pids)
See Photo Tag Table for more information.
Uploading a Photo
Problem
I need to upload a new Facebook photo.
Solution
Use the Photos.upload() method, which currently is not supported in the PHP Client Library (see Adding Missing PHP Client Library Methods). Luckily, Jeremy Blanchard, Paul Wells, and Kevin Pazirandeh have developed an unofficial extension that supports uploading, which you’ll find at http://wiki.eyermonkey.com/Facebook_Photo_Uploads.
If you’re making API calls directly from your own code or are using a different Client Library, this won’t be so important to you. The syntax for their method is very similar to what’s outlined on the Developers Wiki, with the notable exception of the filename argument:
$result = $facebook->api_client->photos_upload($filename, $aid, $caption);
In this case, filename is a URL pointing to the image you want to add to Facebook (it has to be somewhere on the Web already); aid is the album ID you want to add the photo to; and caption is an optional caption describing the image. The Wiki defines this function as accepting raw data to upload the image with, which means you could actually take a file from the user’s local computer and upload it instead of requiring it to be on the Web.
Discussion
Uploading images from a local computer is hard to do properly, due to security constraints. Facebook’s own uploader uses a Java applet to get around sandbox issues that prevent things such as Flash and JavaScript from accessing the local filesystem. With enough experimentation, you should be able to build an HTML form into a page in your Facebook app that makes a multipart submission to your server, including a file upload field, and then you should be able to use this Client Library extension to upload the image into Facebook from there.
Adding Tags to Photos
Problem
I need to add tags to photos that have been stored in the Facebook Photos app.
Solution
Use the Photos.addTag() method, which unfortunately isn’t supported in the PHP Client Library (see Adding Missing PHP Client Library Methods):
$result = $facebook->api_client->photos_addTag('1234567890123456789',