2011-11-03 106 views
2

我需要通过图表api上传一些照片到Facebook,但照片不在同一台服务器上,这可能吗?如何通过图形api上传照片,但当照片在服务器外?

+0

您需要首先将这些照片下载到您的服务器,然后上传它们。 – jlb

+0

我明白@jlb,但客户无法给我访问权限。 – MrSanders

+0

@MrSanders我在下面张贴我的答案后阅读您的评论。您无法通过HTTP(浏览器)或FTP/SCP访问图像? –

回答

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; 
} 
+0

谢谢,实际上我执行了一个类似的过程,但客户(可口可乐,我认为是安全考虑)有一个非常复杂的服务器进程来编写文件,我需要找到任何方式上传该照片无需下载。 (如果还有其他方法!:() – MrSanders

+0

您应该推送到Facebook上的图像已经在客户的服务器上,或者您正在设置某些内容,以便将来的图像将推送到Facebook?无法访问图像,这是不可能的,有或没有Graph API的他们到Facebook上,你能提供关于工作流程的更多细节吗?你的代码将驻留在哪里?在与服务器上的图像相同? –

+0

图像在主服务器之外,我知道这些照片需要放在与api上传相同的服务器上,而我做了这些,但客户不让我在同一台服务器上下载照片,然后我试图找到其他方式上传不要下载他们的照片,但它似乎是不可能的。Facebook不允许通过URL上传照片。 工作流:服务器(其中驻留FB api键) - >请求从外部服务器下载照片(在此st ep客户说,你不需要找出另一种方式) - >一旦下载它们 - > Facebook上传(服务器驻留FB api)。 – MrSanders

0

你确信顾客不会认为你将发布一个链接到他们的服务器,以FB上存在的,这样每个视图将产生在他们的网站流量的照片。如果图像有没有知识产权的关注,那么我认为他们担心其他外部方将跟踪到他们的内部服务器的URL ..

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