简体   繁体   中英

Facebook graph API & PHP SDK questions

I have a couple of problems using facebook graph api.

1). When i try to upload a photo i get the following error "(#324) Requires upload file".

  $attachement = array(
                            'access_token'=> (...)',
                            'name' => 'uploaded foto',
                            'source' => 'C:\Documents and Settings\Username\Desktop\1.jpg'
                    );

        $fb_foto = $fb->api('me/photos','POST',$attachement);

I am sure that source is correct. I have tried with a photo from the internet and not from mu local PC also.

2). How can i delete an object from facebook?. (a wall message for example). I have tried this: $fb->api('/post_id','POST',array('method'=> 'delete'));

But i get "unsupported POST request" error.

Any help would be appreciated.

EDIT: Here is the complete solution i found for upload photos to Facebook. You need to have the version 2.1.1 or above of PHP SDK

 $fb = new Facebook(array(
                        'appId'  => ...,
                        'secret' => ...,
                        'cookie' => true,
        ));

$fb->setFileUploadSupport(true);
      $attachement = array(
                            'access_token'=> '...',
                            'name' => 'uploaded foto',
                            'source' => '@absolute_path_to_the_file'
                    );



      $fb_foto = $fb->api('me/photos','POST',$attachement);

Concerning your 2nd problem, I remember reading somewhere about a DELETE request, instead of POST. See: http://developers.facebook.com/docs/api#deleting

Read the documentation

curl -F 'access_token=...' \
     -F 'source=@file.png' \
     -F 'message=Caption for the photo' \
     https://graph.facebook.com/me/photos

See how the source parameter is formatted?

$fb_foto = $fb->api('me/photos','POST',array(
    'access_token' => (...)
  , 'message'      => 'Caption'
  , 'source'       => '@' . realpath( 'path/to/file' )
));

To delete photos, again the documentation has your answer: Issue a DELETE request

$fb->api( '/PHOTO_ID', 'DELETE' );

I'm using this method with the new php-sdk (v2.1.1, facebook-php-sdk-v2.1.1-0-g08909f3.zip) to upload a photo for a new event. The php code below creates the event.

$fname="/tmp/foo.jpg";
$attachment =  array(
        'access_token' => $facebook->getAccessToken(),
        'name' => substr(event_name),
        'description' => my_description,
        'start_time' => my_start_time,
        'link' => my_link,
        'source'=> '@'.$fname
        );
$result = $facebook->api('/me/events', 'POST', $attachment);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM