微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

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

Upload Photo To Album with Facebook’s Graph API

How can I upload photos to album using Facebook Graph API

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

这是我的代码

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

…其中attachment-> guid包含照片网址.

我现在几乎被困住了……

解决方法:

我认为问题出在这里

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

其余看起来很好

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐