2011-08-19 115 views
2

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

而不是使用

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

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

取而代之的是的realpath($文件)我想用远程路径的图像,例如:

http://myserver.com/image.jpg 

我试图取代realpath($文件)与http链接,但我得到以下错误:

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

回答

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 
}