问题通过Facebook的API上传照片到相册 - PHP

问题描述:

我有问题由 Facebook的API 上传照片到相册。这是我的code。

I have problems to upload a photo to an album by the facebook API. this is my code.

$facebook->setFileUploadSupport(true);

    //Create an album
    $album_details = array(
            'message'=> 'Message',
            'name'=> 'Album Name'
    );
    $create_album = $facebook->api('/me/albums?access_token='.$access_token, 'post', $album_details);

    //Get album ID of the album you've just created
    $album_id = $create_album['id'];

    echo $album_id." - ";

    //Upload a photo to album of ID...
    $photo_details = array();

    $img = "app.jpg";

    $photo_details['source'] = '@' . $img;
    $photo_details['message'] = 'Wow.. cool image!';

    $upload_photo = $facebook->api('/'.$album_id.'/photos?access_token='.$access_token, 'post', $photo_details);

当我与表单上传图片,它的作品!但这code不将图像上传至相簿中。
我也试图与卷曲但并没有什么...我不知道问题出在哪里?

When i upload the image with a form, it works! but this code does not upload the image into the album. I have tried also with CURL but there is nothing... i don't know where the problem is...

在图形API资源管理器中测试一些东西后,这里的工作PHP版本:

After testing few things on Graph API Explorer, Here's a working PHP Version:

<?php
# Path to facebook's PHP SDK.
require_once("facebook.php");

# Facebook application config.
$config = array(
    'appId'      => 'YOUR_APP_ID',
    'secret'     => 'YOUR_APP_SECRET',
    'fileUpload' => true # Optional and can be set later as well (Using setFileUploadSupport() method).
);

# Create a new facebook object.
$facebook = new Facebook($config);

# Current user ID.
$user_id = $facebook->getUser();

# Check to see if we have user ID.
if($user_id) {

  # If we have a user ID, it probably means we have a logged in user.
  # If not, we'll get an exception, which we handle below.
  try {

    # Get the current user access token:
    $access_token = $facebook->getAccessToken();

    # Create an album:
    $album_details = array(
            'access_token' => $access_token,
            'name'         => 'Album Name',
            'message'      => 'Your album message goes here',
    );

    $create_album = $facebook->api('/me/albums', 'POST', $album_details);

    # Get album ID of the album you've just created:
    $album_id = $create_album['id'];

    # Output album ID:
    echo 'Album ID: ' . $album_id;

    # Upload photo to the album we've created above:
    $image_absolute_url = 'http://domain.com/image.jpg';

    $photo_details = array();
    $photo_details['access_token']  = $access_token;
    $photo_details['url']           = $image_absolute_url;                        # Use this to upload image using an Absolute URL.
    $photo_details['message']       = 'Your picture message/caption goes here';

    //$image_relative_url             = 'my_image.jpg';
    //$photo_details['source']        = '@' . realpath($image_relative_url);      # Use this to upload image from using a Relative URL. (Currently commented out).

    $upload_photo = $facebook->api('/' . $album_id . '/photos', 'POST', $photo_details);

    # Output photo ID:
    echo '<br>Photo ID: ' . $upload_photo['id'];

  } catch(FacebookApiException $e) {
    // If the user is logged out, you can have a 
    // user ID even though the access token is invalid.
    // In this case, we'll get an exception, so we'll
    // just ask the user to login again here.
    $login_url = $facebook->getLoginUrl( array('scope' => 'publish_stream, user_photos')); 
    echo 'Please <a href="' . $login_url . '">login.</a>';
    error_log($e->getType());
    error_log($e->getMessage());
  }   
} else {

  # No user, print a link for the user to login and give the required permissions to perform tasks.
  $params = array(
      'scope' => 'publish_stream, user_photos', # These permissions are required in order to upload image to user's profile.
  );

  $login_url = $facebook->getLoginUrl($params);
  echo 'Please <a href="' . $login_url . '">login.</a>';

    }
?>

我加入了注释,所以你可以理解它通过读取code。

I have added comments so you could understand what it does by reading the code.

这工作既绝对URL和相对URL,我都注释掉code,使用与您在您的意见所提到的相对URL您无法读取图像的真实路径上传图像。

This works with both absolute url and relative url, I have commented out code for uploading image using relative url as you have mentioned in your comments you can't read real path of the image.

编辑:注意:用户必须提供延长权限Facebook应用程序上传图片到他们的个人资料,这些权限 publish_stream user_photos

Note: The user has to give extended permissions to your facebook application to upload images to their profile, Those permissions are publish_stream and user_photos.

让我知道,如果这有助于你,如果它的工作原理:)

Let me know if this helped you and if it works :)