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.

这是我的代码: p>

Here's my code:

      /*
  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.

我现在很困难了...

I'm pretty much stuck right now...

我认为问题在这里:

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

其余的看起来很好,虽然

the rest looks fine though