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

时间:2012-09-24 09:28:56

标签: facebook api facebook-graph-api opengraph

我在facebook API上传照片到相册时遇到问题。这是我的代码。

$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);

当我使用表单上传图片时,它可以正常工作!但是此代码不会将图像上传到相册中。 我也试过CURL,但没有...我不知道问题出在哪里......

2 个答案:

答案 0 :(得分:4)

在Graph API Explorer上测试了一些东西之后,这是一个可用的PHP版本:

<?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>';

    }
?>

我添加了评论,因此您可以通过阅读代码了解它的作用。

这适用于绝对网址和相对网址,我已经注释了使用相对网址上传图片的代码,正如您在评论中提到的那样,您无法读取图片的真实路径。

编辑:注意:用户必须为您的Facebook应用程序提供扩展权限才能将图片上传到其个人资料中,这些权限为publish_streamuser_photos

请告诉我这是否对您有所帮助以及是否有效:)

答案 1 :(得分:-1)

    $user = $this->facebook->getUser();
        $this->facebook->setFileUploadSupport(true);
        $user_profile = $this->facebook->api('/me');
        $album_details = array(
            'message' => 'Hello everybody this is me ' . $user_profile['name'],
            'name' => 'I am so slim because I dont have money to eat....:('
        );
        $create_album = $this->facebook->api('/me/albums', 'post', $album_details);

// Upload a picture
        $photo_details = array(
            'message' => 'I am so slim because I dont have money to eat....:('
        );
        $photo_details['image'] = '@' . realpath('./a.jpg');
        $upload_photo = $this->facebook->api('/' . $create_album['id'] . '/photos', 'post', $photo_details);

请在$ this-&gt; facebook上使用$ facebook