Take the 2-minute tour ×

Possible Duplicate:
Add picture at facebook event with Graph API

I have been trying for a good few hours now to create an event through the Facebook API with an image. So far I have been able to create events though both the Graph and Rest APIs without images, but have been unable to attach any images.

I believe the REST API is the only API which supports attaching images, but the documentation is pretty poor and the php-sdk docs or code do not help much either.

My latest attempt is over at: http://promos.uk.glam.com/splash_test/example.php

With the code: http://pastebin.com/8JC8RAck (note the "require facebook.php" is the php-sdk - http://github.com/facebook/php-sdk/)

Note line 93 ( "if ($uid) {" ), this is supposed to be "if ($me) {", which was working for an hour or so, and then stopped working (no changes to the code that populates $me), odd.

So, the event creation code is lines 96-99 and as it is now it creates an event without an image, but as soon as I put back in the commented out code (line 98, ) it fails to do anything.

Does anyone know how to create events on facebook though the API with images? If so, please help me out here! I have no problems scrapping all this code if there is another solution, although I have to use PHP or Javascript.

Thanks all

share|improve this question

marked as duplicate by casperOne Apr 26 '12 at 19:22


4 Answers 4

up vote 10 down vote accepted

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 the Facebook object allowing file upload
$facebook = new Facebook(array(
  'appId'  => 'xxxxxxxxxxxxxxxxxxxx',
  'secret' => 'xxxxxxxxxxxxxxxxxxxx',
  'cookie' => true,
  'fileUpload' => true


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

share|improve this answer

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);

echo "Event ID= " . $result;
share|improve this answer

The following code works for me, using the PHP SDK.

$attachment = array(
  'message' => $caption,
$attachment[basename($file_path)] = '@' . realpath($file_path);
$result = $facebook->api('me/photos','post',$attachment);

To do it with raw curl, see this answer.

share|improve this answer

You can post images to facebook like:

// variables required in this example:
$img_path = "/home/www/test.png"; // image path
$img_caption = "testing"; // caption
$album_id = "12345678910"; // album id
$facebook_uid = "555555555"; // facebook user id
$facebok_user_access_token = "ajsdhflhasdf|sjdhflkasdf."; // facebook user token 

//upload photo
$args = array(
    'aid'       => $album_id,       // album id, possibly event id?
    'caption'   => $img_caption,    // caption for image
    'uid'       => $facebook_uid,   // facebook user id of the poster
    'format'    => 'json'
$args[basename($img_path)] = '@' . realpath($img_path);
$ch = curl_init();
$url = "https://api.facebook.com/method/photos.upload?access_token={$facebook_user_access_token}";
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);
$upload_result = curl_exec($ch);

This is how you can use the API to upload photos to facebook albums using PHP. Not exactly certain that it works for events also. I would test it out but I am unable to access facebook from work.

Hope this is a good start for you.

share|improve this answer
