2011-08-19 114 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 
}