1

I've searched around everywhere without luck and found lots of examples to upload a photo to facebook when the user is in a session (i.e. the user is physically sitting at the computer and accessing the web page). I've tried the samples, and they work.

I noticed this unanswered question from last year on the same issue Stackoverflow Question

My current app lets the user authorise off-line updates and I store the access_token, user_id, etc. and I can succesfully post to a users wall when they are offline.

I'm really struggling getting something to work with posting a photo to the users wall. Reading the Facebook documentation, I'm thinking you can only upload photos using multipart/form-data!?!?

That wouldn't work if the user isn't at their computer. Can you upload photos that are stored on a directory on my server?

Here's my code so far. Remember, this doesn't use a facebook session as the access_code has already been granted and stored beforehand. As I mentioned, posting to a users wall already works with this approach.

$filename= "@/myphotodir/filename.jpg");
$url = "https://graph.facebook.com/".$uid."/photos";  //$uid is fb user id
$ch = curl_init($url);
$attachment =  array('access_token' => $access_token,
                'app_id'            => $app_id,
                'name'              => "A photo from me...",
                'fileUpload'        => true,
                'message'           => "my message",
                'image'             => $filename,
          );
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result= curl_exec($ch);
curl_close ($ch);

Edit: $result comes back false... forgot to add that.

Any help would be appreciated. Many thanks, Dean

Community
  • 1
  • 1
Dean
  • 533
  • 2
  • 5
  • 10

1 Answers1

0

You should use the Facebook SDK. With the Facebook SDK, you can use:

$facebook->setFileUploadSupport( true );
$parameters = array(
    'access_token' => 'ACCESS_TOKEN_HERE',
    'message' => 'PHOTO_CAPTION',
    'image' => '@' . realpath( '/path_to_file.jpg' ) // Notice the @ sign
);
$facebook->api( '/user_id/photos', 'post', $parameters );

This will post the photo to their default album. If you replace user_id with an album_id you can post to a specific album.

Colin M
  • 13,010
  • 3
  • 38
  • 58
  • I tried this before and it didn't work. However, I did try again in case I missed something and the Facebook SDK is expecting Form Post Data (i.e. it is expecting you to be sitting at your machine, uploading a file via a form). In offline upload mode there will not be any form post data. The exact error message from the SDK is as follows. ----> " Fatal error: Uncaught CurlException: 26: failed creating formpost data thrown in /home/myforev3/public_html/library/facebook.php on line 629" – Dean Apr 18 '11 at 09:33
  • I want to do the same thing. @Dean, did you find any solution. Please please let me know if so. Thanks in advance – Shahid Karimi Mar 12 '13 at 12:22