PHP - 通过 Graph API 将网络托管的照片上传到 Facebook 相册

问题描述:

我正在尝试上传 www 托管(例如 http://www.google.se/intl/en_com/images/srpr/logo1w.png) 文件到 Facebook 相册.

I'm trying to upload www hosted (e.g. http://www.google.se/intl/en_com/images/srpr/logo1w.png) files to a facebook album.

创建相册工作正常,但我似乎没有上传任何照片.我正在使用 facebook php-sdk ( http://github.com/facebook/php-sdk/ ) 和我已经尝试过的例子是:

Creating an album works just fine, but I don't seem to uploading any photos. I'm using the facebook php-sdk ( http://github.com/facebook/php-sdk/ ) and the examples I already tried are:

使用 Facebook 的 Graph API 将照片上传到相册

我该怎么办使用 Facebook Graph API 将照片上传到相册

我猜 CURL 上传可能只能管理本地存储的文件,而不能管理网络托管的文件.

I'm guessing CURL uploads perhaps only can manage locally stored files and not web hosted ones.

这是我的代码:

      /*
  Try 1:

  $data = array();
  $data['message'] = $attachment->post_title;
  $data['file'] = $attachment->guid;

  try {
   $photo = $facebook->api('/' . $album['id'] . '/photos?access_token=' . $session['access_token'], 'post', $data);
  } catch (FacebookApiException $e) {
   error_log($e);
  }
  */

  // Try 2:
  //upload photo
  $file = $attachment->guid;
  $args = array(
   'message' => 'Photo from application',
  );
  $args[basename($file)] = '@' . realpath(file_get_contents($file));

  $ch = curl_init();
  $url = 'https://graph.facebook.com/' . $album['id'] . '/photos?access_token=' . $session['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
  print_r(json_decode($data,true));

...其中附件->guid 包含照片网址.

...where attachment->guid contains the photo url.

我现在很困...

我认为问题出在这里:

$args[basename($file)] = '@' . realpath(file_get_contents($file));

由于您想发布来自其他来源的图片(对吗?),您应该将其暂时保存在您自己的主机上.

since you want to post a picture from another source (right?), you should save it temporarily on your own host.

我也需要做这样的事情,因为我必须处理图像,所以我使用了以下方式:

i also needed to do something like this, and since i had to process the image, i used the following way:

$im = @imagecreatefrompng('http://www.google.se/intl/en_com/images/srpr/logo1w.png');
imagejpeg($im, 'imgs/temp/temp.jpg', 85);

$args['image'] = '@' . realpath('imgs/temp/temp.jpg');

其他的看起来还不错