1

My app is posting to the users Facebook feed. Everything works fine, but the picture is missing. I cannot figure out why.

This is the array used by the $facebook->api method:

Array
(
    [message] => MyText
    [link] => MyLink
    [name] => MyName
    [picture] => https://lala.herokuapp.com/images/oceanblue.png
    [caption] => MyCaption
    [description] => MyDescription
)

The image is accessible (not the real url above) and shows up when I open the url within a browser.

The api return object looks like this:

Array
(
    [id] => 652685341_10151011701170342
)

Help is much appreciated, thanks! :)

Kevin Bedell
  • 13,254
  • 10
  • 78
  • 114
  • curl -I 'https://lala.herokuapp.com/images/oceanblue.png' HTTP/1.1 404 Object Not Found {edit} wait, i just realised you didn't include the real image, let me check... – Igy Jun 13 '12 at 10:28
  • real url: https://evening-wind-6067.herokuapp.com/images/ocean-blue.png –  Jun 13 '12 at 10:41
  • can be this? http://stackoverflow.com/a/5572622/1049668 – Luca Rainone Jun 13 '12 at 12:23

1 Answers1

4

I wanted to do the same thing. You can't upload a remote picture to facebook, you need to have the picture on your server so I download the image on my server and then upload it. Here's my code :

/**
 * Post photo on facebook
 * This function requires the user to be logged in
 * This function requires the 'publish_stream' permission
 * 
 * Fetches the picture from remote URL and uploads it to facebook
 * @param string $picture Picture URL
 * @param string $description Description to place in caption
 * @param string $link Link to place in caption
 * @param string $albumId Id of the previously created album
 */
function postPhotoOnFacebook($picture, $description, $link, $albumId){
    global $userId, $facebook;

    $tempFilename = $_SERVER['DOCUMENT_ROOT'].'/temp/';
    $tempFilename .= uniqid().'_'.basename($picture);

    if ($userId) {
        try {
        if($imgContent = @file_get_contents($picture)){
            if(@file_put_contents($tempFilename, $imgContent)){
                $photo_details = array('message' => "$link $description");
                $photo_details['image'] = '@' . realpath($tempFilename);
                $data = $facebook->api('/'.$albumId.'/photos', 'post', $photo_details);

                unlink($tempFilename);
            }
        }
        } catch (FacebookApiException $e) {
            error_log($e);
            $userId = null;
        }
    }
}

This stack question helped a lot : Upload a remote photo to an upload

Hope that helps!

Community
  • 1
  • 1
William Fortin
  • 777
  • 1
  • 4
  • 17