Question

I have a PHP script that upload an image+description+other details on my facebook fanpage wall. But what i need is to upload INSIDE an Album. I'm looking all over the internet for this answer, this tutorial is the closest i got until now.

Here's my code:

$params = array(
    "access_token"  => $page_token,
    "message"       => "#php #facebook",
    "link"          => "www.domain.com.br",
    "description"   => "Buy this now!",
    "source"        => "@" . $path,
);

try {
  $ret = $fb->api('/'.$album_id . '/photos', 'POST', $params);
  echo 'Successfully posted to Facebook Fan Page';
} catch(Exception $e) {
  echo $e->getMessage();
}

I have already tried to change this $ret = $fb->api('/'.$fanpage_id . '/photos', 'POST', $params); path to this one: $ret = $fb->api('/'.$fanpage_id.'/'.$album_id . '/photos', 'POST', $params); and something similar.

But it returns (#240) The target_id references an inactive user because fb API thinks that $album_id is the $fanpage_id.

if somebody knows how to post a picture inside the album, please help me ;)

Was it helpful?

Solution

Here it is, this code checks if the album with the name you want exists, if not creates a new one:

//Check if the album exists just change PAGE_ID to the ID of your page and NAME_OF_YOUR_ALBUM to the name of the album where you want to upload the photo
$fql = "SELECT object_id FROM album WHERE owner = PAGE_ID AND name='NAME_OF_YOUR_ALBUM'";
$album_exists = $this->facebook->api(array(
    'method' => 'fql.query',
    'query' =>$fql,
    'access_token' => $accessToken
));
if(array_key_exists(0, $album_exists)){
    //the album with that name exists, let's save his ID
    $album_uid = $album_exists[0]['object_id'];
}else{
    //We don't have an album with that name, let's create an album
    $album_details = array('name'=> 'NAME_OF_YOUR_ALBUM');
    $create_album =  $this->facebook->api('/'.$page_id.'/albums', 'post', $album_details);
    //Get album ID of the album you've just created
    $album_uid = $create_album['id'];
}
//details of the photo you want to upload
$photo_details = array('message'=> 'This is a fantastic photo');
$file='PATH_TO_YOUR_FILE/NAME_FILE.jpg'; //Example image file
$photo_details['image'] = '@' . realpath($file); //get the real path
//Upload the photo to the album you've just created or the one you wanted
$upload_photo =  $this->facebook->api('/'.$album_uid.'/photos', 'post', $photo_details);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top