如何通过图形api上传照片,但是当照片在服务器外面时?

时间:2011-11-03 14:07:49

标签: facebook facebook-graph-api

我需要通过图形api将一些照片上传到Facebook,但照片不在同一台服务器上,有可能吗?

2 个答案:

答案 0 :(得分:0)

这是可能的,但需要存储本地副本的中间步骤(如@jlb所述)。您只需要要提取的图像的URL以及Facebook上的相应权限。试试这个:

function pushImageToFacebook( $url_of_image , $fb_user_id , $fb_access_token )
{
  // Creates a new image file in the local directory and gets
  // ...a file handle for it.  MAKE SURE that you are able to
  // ...write to and read from the local directory or this 
  // ...will fail due to permissions.
  $fh = fopen( $filename = 'image' . microtime() , 'w' );
  // Writes the image data to the local file
  fwrite( $fh , file_get_contents( $url_of_image ) );
  // Closes the file handle
  fclose( $fh );

  // Sets the message argument
  $args = array( 'message' => 'Photo Uploaded Using cURL' );
  // Sets the filename argument
  $args[basename($filename)] = '@' . realpath($filename);
  // Initializes a cURL session and gets a cURL handle
  $ch = curl_init();
  // This is your Facebook Graph API URL
  $url = "https://graph.facebook.com/{$fb_user_id}/photos?access_token={$fb_access_token}";
  // Set all of your cURL session options
  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);
  // Fire the cURL and grab the return data (Facebook image ID)
  $buffer = curl_exec($ch);

  // OPTIONAL : Display the Facebook image ID
  print_r( json_decode( $buffer , true ) );

  return;
}

答案 1 :(得分:0)

您确定客户不会认为您会将其服务器上存在的照片的链接发布到FB,以便每个视图都会在其网站上生成流量。如果图像没有IP问题,那么我就是认为他们担心其他外部各方会关注其内部服务器的URL ..

或者,让客户通过电子邮件向您发送照片,然后使用cron脚本轮询INBOX,然后从邮箱下载并在收到时上传到FB。您需要解决安全问题,例如有人找到电子邮件地址并发送不应该放在FB上的照片。