2

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.

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:

Upload Photo To Album with Facebook's Graph API

How can I upload photos to album using Facebook Graph API

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

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

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

I'm pretty much stuck right now...

4

3 回答 3

2

我认为问题出在这里:

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

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

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

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

其余的看起来不错

于 2011-05-26T10:56:22.937 回答
0

我建议使用Facebook php SDK,它会更容易,并且代码将适用于 API 的未来更新:


使用图形 API php sdk:

$fbk = new Facebook(/* conf */);
$fbk->setFileUploadSupport(true);

//If you are executing this in a script, and not in a web page with the user logged in:
$fbk->setAccessToken(/* access token from other sources */);

//To add to an album:
$fbk->api("/$albumId/photos", "POST", 
          array('source' => '@'. realpath($myPhoto), 'message'  => "Nice photo"));

//To upload a photo directly (the album will be created automatically):
$fbk->api("/me/photos", "POST", 
          array('source' => '@'. realpath($myPhoto), 'message'  => "Nice photo"));

直接使用 cURL:

如果您真的想使用 cURL,您的代码几乎是正确的,但错误在 $args 数组中:

$args = array(
   'message' => 'Photo from application',
   'source' => file_get_contents($file)
  );

由于照片数据的键是source,请参阅Facebook Doc


注意 cURL 中的 @:

另请注意,@在 cUrl 中表示该参数将被替换为 后面的文件的实际字节@,因此如果您已经将实际字节放入参数中,则不需要source

于 2013-05-03T09:23:19.073 回答
0

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

不,事实并非如此。

但是您需要为图像提供完整的、可公开访问的 HTTP URL,@前面没有 - 并且您必须使用url该值的参数名称。

https://developers.facebook.com/docs/reference/api/photo/

您还可以通过提供url带有照片 URL 的参数来发布照片。

于 2013-05-03T10:04:38.227 回答