如何使用图表API将照片上传到相册

时间:2011-09-12 17:54:44

标签: php facebook-graph-api

我的代码(不工作):

<?php
require '../src/facebook.php';
$facebook = new Facebook(array(
  'appId'  => '...',
  'secret' => '...'
));

Facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYPEER] = false; 
$user = $facebook->getUser();
if ($user) {
  try {
    $user_profile = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    error_log($e);
    $user = null;
  }
}

if ($user) {
  $album_id = '246592692052269';    
  $FILE_PATH = 'Language.jpg';
  $access_token = $_SESSION['fb_appId_access_token'];
  $logoutUrl = $facebook->getLogoutUrl();
   $args = array('image'=> '@' . __DIR__.$FILE_PATH, 
           'name' => 'this photo was taken in acre',
          'message'=> 'Photo Caption'); 
    $ch = curl_init();
    $url = 'http://graph.facebook.com/'.$album_id.'/photos?access_token='.$access_token;
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
    $data = curl_exec($ch);
    //returns the photo id
    if(empty($data)) {
    echo 'Responde: '. $data . '<pre>';
    print_r(curl_getinfo($ch));
    echo '</pre>';
    } else {
    echo 'Upload failed! erro='. curl_error($ch);
    }

} else {
  $loginUrl = $facebook->getLoginUrl();
}

?>

输出:

Array
(
    [url] => http://graph.facebook.com/246592692052269/photos?access_token=...
    [content_type] => 
    [http_code] => 0
    [header_size] => 0
    [request_size] => 0
    [filetime] => -1
    [ssl_verify_result] => 0
    [redirect_count] => 0
    [total_time] => 0.203
    [namelookup_time] => 0
    [connect_time] => 0.203
    [pretransfer_time] => 0.203
    [size_upload] => 0
    [size_download] => 0
    [speed_download] => 0
    [speed_upload] => 0
    [download_content_length] => -1
    [upload_content_length] => -1
    [starttransfer_time] => 0
    [redirect_time] => 0
)

有人可以指出我的错误吗?提前谢谢。

2 个答案:

答案 0 :(得分:1)

确保在服务器端启用文件上传,并按照PHP SDK documentation页面上的说明将fileUpload设置为true初始化SDK,或致电setFileUploadSupport。您不需要像上面那样使用访问令牌和curl命令手动调用图API,PHP SDK将为您解决这个问题。有关完整示例,请参阅api method documentation页面。

答案 1 :(得分:0)