-2

I'm trying to upload images to face book using php. But I'm getting this error now "Uncaught OAuthException: (#324) Requires upload file thrown in".

$app_id = "257711244318488";
$app_secret = "dc4b279079e4eafdfgdfgdfgf6e5b9ef37fb48402";
$facebook = new Facebook(array(
         'appId' => $app_id,
         'secret' => $app_secret,
         'cookie' => true
        ));
$fbcheck= $facebook->getUser();

if(is_null($fbcheck) or !$fbcheck or $fbcheck==0)
    {
     header("Location:{$facebook->getLoginUrl(array('scope' => 'user_status,publish_stream,user_photos'))}");
     exit;
    }
if($_SERVER['REQUEST_METHOD'] =='POST'){
    //$img = realpath($_FILES["pic"]["tmp_name"]);
    $facebook->setFileUploadSupport("http://" . $_SERVER['SERVER_NAME']);
    $photo = $facebook->api('/me/photos', 'POST', 
    array(
            'source' =>  $urlParser->fbpostpic($input['ik']),
            'message' => 'This photo was uploaded via www.Picslanda.com'
    )
    );

            echo '<p><a target="_blank" href="http://www.facebook.com/photo.php?fbid='.$photo['id'].'">Click here to watch this photo on Facebook.</a></p>';
}

$attachment = array(
    'message' => 'Hello friends  ',
    'name' => "My Name",
    'caption' => "My caption",
    'link' => "www.site.com",
    'description' => 'Description.',
    'picture' => "url",
    'actions' => array(array(
        'name' => 'Get Search',
        'link' => 'http://www.google.com'
        ))
        );
    $result = $facebook->api(
    '/me/feed/',
    'post',
    $attachment
    );
Cœur
  • 37,241
  • 25
  • 195
  • 267
sunil kumar
  • 35
  • 3
  • 8

3 Answers3

4

First , You need a permission photo_upload

it should be

array('scope' => 'user_status,publish_stream,user_photos','photo_upload')

Second

you need to enable file upload in php sdk

$facebook->setFileUploadSupport(true);

if you want to upload photo, you should use @ in file path

// Upload a photo to a user’s profile
// Your app needs photo_upload permission for this to work
$facebook->setFileUploadSupport(true);

$img = '/tmp/mypic.png';

$photo = $facebook->api('/me/photos', 'POST',
                        array( 'source' => '@' . $img,
                               'message' => 'Photo uploaded via the PHP SDK!'
                       ));

check more information at https://developers.facebook.com/docs/reference/php/facebook-setFileUploadSupport/

saturngod
  • 24,649
  • 17
  • 62
  • 87
0

The problem might be that you don't have an @ in 'source'=> '@' . filepath. When I leave it out, I get your 324 error.

Unfortunately, I am getting an uncaught Oauth Exception #1 when I put it in.

Lyuben Todorov
  • 13,987
  • 5
  • 50
  • 69
0

You need to add photo_upload to your requested scopes to upload images.

$facebook->getLoginUrl(array('scope' => 'user_status,publish_stream,user_photos,photo_upload'))

Also, the app_secret is supposed to be secret, so publishing it on the Internet is probably not a good idea, since it lets anyone upload photos under your app's name.

Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294
  • Thanks for your replay , but now Iam getting this error "Uncaught OAuthException: (#324) Requires upload file thrown in ". Please help me – sunil kumar Apr 03 '12 at 18:29
  • Iam little confuced about the image path portion array( 'source' => 'http://www.mysite,com/picture.png', 'message' => 'msg' ). Please chek the source. Is it correct ? – sunil kumar Apr 03 '12 at 18:37
  • `source` is supposed to be the file system path (preferably absolute) where the file is located, not an URL. For example `/tmp/picture.png` would be a good path if the file was located in `/tmp`. – Joachim Isaksson Apr 03 '12 at 18:38
  • The image is in the server. I would like to use the full image path in the source index. ( I mean no need to upload it ). Please help me . It is really urgent.. – sunil kumar Apr 03 '12 at 18:39
  • So need to use the realpath command or just the physical path of the picture. Picture is uploaded in the uploads/foldername.picturename.png. So can I use 'source' => "uploads/foldername.picturename.png",. Please help me. – sunil kumar Apr 03 '12 at 18:44
  • @sunilkumar Yes, you can use that path, but preferably use an _absolute_ path, ie one starting with `/`. If your uploads folder is placed in `/tmp` for example, use `/tmp/uploads/foldername.picturename.png`. – Joachim Isaksson Apr 03 '12 at 18:50
  • But for security reasons, Iam just uploading the files with out any extension . The file extension is in the data base. So what I can do with that pfysical path ? – sunil kumar Apr 03 '12 at 18:51
  • I don't understand your question. You need to make `source` point to the exact location, _including path and any extensions_ in the file system of the file you want to upload. If you want to upload it by another name, you as far as I know need to rename it before uploading. – Joachim Isaksson Apr 03 '12 at 19:01