Attach image to Facebook event (php sdk, rest or graph api)

Thanks everyone for your answers, I decided to revisit this to see if the API is working correctly now. It is! Uploading pictures with the Graph API now works. Thanks to Stan James for bringing my attention to this and giving code to post photos, which I adapted for events:

Setup:

//Setup the Facebook object allowing file upload
$facebook = new Facebook(array(
  'appId'  => 'xxxxxxxxxxxxxxxxxxxx',
  'secret' => 'xxxxxxxxxxxxxxxxxxxx',
  'cookie' => true,
  'fileUpload' => true
));

Post:

//Path to photo (only tested with relative path to same directory)
$file = "end300.jpg";
//The event information array (timestamps are "Facebook time"...)
$event_info = array(
    "privacy_type" => "SECRET",
    "name" => "Event Title",
    "host" => "Me",
    "start_time" => 1290790800,
    "end_time" => 1290799800,
    "location" => "London",
    "description" => "Event Description"
);
//The key part - The path to the file with the CURL syntax
$event_info[basename($file)] = '@' . realpath($file);
//Make the call and get back the event ID
$result = $facebook->api('me/events','post',$event_info);

I have put all the code on pastebin (http://pastebin.com/iV0JL2mL), it's a bit messy from my debugging and getting angry with Facebook months ago! But it works.


This seems to work

$file = "pj100.jpg";
$args = array(
    'access_token' => 'your access token',
    'name' => 'Event name',
    'description' => 'The Description ',
    'start_time' => '2012-09-01T12:00:00+0000',
    '@file.jpg' => '@' . realpath($file)); 
$target_url = "https://graph.facebook.com/me/events";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $target_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
$result = curl_exec($ch);
curl_close($ch);

echo "Event ID= " . $result;

Tags:

Php

Facebook