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

Get Facebook Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.