November
27th, 2008
Using the Facebook API to upload photos
Facebook’s photos and albums are no way to truly store or organize your photos. It is however one of the best ways to share them. This created quite a conundrum for me as I had no interest in uploading photos to two different websites. I don’t use Flickr or Smugmug or any other photo sharing service that you’ve heard of. Since I was a co-founder at Photagious it’s a natural choice as my photo management site.
Defining the goal
I wanted to be able to selectively publish photos from my Photagious account to Facebook. I didn’t need any of the fancy Facebook application junk…I just wanted to make it easy to post photos from my Photagious account into my Facebook photos. Turns out that it’s dead simple once you do a little bit of research.
Step number one
First, you need to get an application key and secret. One way to do this by creating a new application. Most of the information you’ll be asked for will be unused, so fill them in with whatever you’d like. Just make sure to enable developers working on the application to install it.
Step number two
Second, you need to install the application.
Step number three (final)
Now, the code. Basically, you generate the request with the required fields, generate the signature, append the file and send off the request. For the sake of brevity, I’m pasting in the entire file here.
$key = 'your_key';
$sec = 'your_secret';
$ver = '1.0';
$cid = microtime(true);
$uid = 'BIGINT';
$file= '/path/to/your/file.jpg';
$args = array(
'method' => 'photos.upload',
'v' => $ver,
'api_key' => $key,
'uid' => $uid,
'call_id' => $cid,
'format' => 'XML'
);
signRequest($args, $sec);
$args[basename($file)] = '@' . realpath($file);
$ch = curl_init();
$url = 'http://api.facebook.com/restserver.php?method=photos.upload';
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
$data = curl_exec($ch);
echo $data;
function signRequest(&$args, $secret){
ksort($args);
$sig = '';
foreach($args as $k => $v){
$sig .= $k . '=' . $v;
}
$sig .= $secret;
$args['sig'] = md5($sig);
}
Seriously, that’s it. Here’s the output I received from the command line.
jmathai@[~/Y/photos]: php fb.php <?xml version="1.0" encoding="UTF-8"?> <photos_upload_response xmlns="http://api.facebook.com/1.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://api.facebook.com/1.0/ http://api.facebook.com/1.0/facebook.xsd"> <pid>XXXXXXXXXXXXXXXXXX</pid> <aid>XXXXXXXXXXXXXXXXXX</aid> <owner>XXXXXXXXXXX</owner> <src>http://photos-d.ak.fbcdn.net/photos-ak-sf2p/v645/2/72/500273081/s500273081_1648227_6794.jpg</src> <src_big>http://photos-d.ak.fbcdn.net/photos-ak-sf2p/v645/2/72/500273081/n500273081_1648227_6794.jpg</src_big> <src_small>http://photos-d.ak.fbcdn.net/photos-ak-sf2p/v645/2/72/500273081/t500273081_1648227_6794.jpg</src_small> <link>http://www.facebook.com/photo.php?pid=1648227&id=500273081</link> <caption/> <created>1227781696</created> <story_fbid>0</story_fbid> </photos_upload_response>
Additional reading
You’ll want to get more familiar with Facebook’s API and some of the nuances when posting photos. Here are some links to help get you on your way.

January 2nd, 2009 at 8:40 pm
I tried this sample and it just times out, has something changed in the API since you posted the code?