将远程照片上传到上传

时间:2011-08-19 02:33:04

标签: php facebook facebook-graph-api

是否可以使用Facebook PHP库将远程图片上传到Facebook?

而不是使用

    $facebook->setFileUploadSupport(true);
    $args = array('message' => 'My Caption');
    $args['image'] = '@' . realpath($file);

    $data = $facebook->api('/me/photos', 'post', $args);

而不是 realpath($ file)我想使用图片的远程路径,例如:

http://myserver.com/image.jpg

我尝试用http链接替换 realpath($ file)但是我收到以下错误:

 Uncaught CurlException: 26: couldn't open file "http://mydomain.com/fb_images/EwrTsUqEuG.jpg"

1 个答案:

答案 0 :(得分:3)

使用file_get_contents()将文件下载到您的服务器,然后file_put_contents()将其存储在临时的本地文件中以进行上传FB传输过程,然后unlink()删除该文件

<?php

# The URL for the Image to Transfer
$imageURL = 'http://server.com/the_image.jpg';
# Folder for Temporary Files
$tempFilename = $_SERVER['DOCUMENT_ROOT'].'/tempFiles/';

# Unique Filename
$tempFilename .= uniqid().'_'.basename( $imageURL );

# Get the Image
if( $imgContent = @file_get_contents( $imageURL ) ){
  if( @file_put_contents( $tempFilename , $imgContent ) ){

    $facebook->setFileUploadSupport(true);
    $args = array('message' => 'My Caption');
    $args['image'] = '@' . realpath( $tempFilename );

    $data = $facebook->api('/me/photos', 'post', $args);

    # Once done, delete the Temporary File
    unlink( $tempFilename );

  }else{
    # Failed to Save Image
  }
}else{
  # Failed to Get Image
}