本文介紹了上傳遠程照片到上傳的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
是否可以使用 Facebook PHP 庫將遠程圖片上傳到 Facebook??
Is it possible to upload a remote picture to Facebook using the Facebook PHP Library??
代替使用
$facebook->setFileUploadSupport(true);
$args = array('message' => 'My Caption');
$args['image'] = '@' . realpath($file);
$data = $facebook->api('/me/photos', 'post', $args);
代替 realpath($file) 我想使用圖像的遠程路徑,例如:
Instead of a realpath($file) I would like to use a remote path to an image for example:
http://myserver.com/image.jpg
我嘗試用 http 鏈接替換 ??realpath($file),但出現以下錯誤:
I tried to replace the realpath($file) with a http link but I got the following error:
Uncaught CurlException: 26: couldn't open file "http://mydomain.com/fb_images/EwrTsUqEuG.jpg"
推薦答案
使用 file_get_contents() 將文件下載到您的服務器,然后 file_put_contents() 將其存儲在一個臨時的本地文件中,用于上傳 FB 傳輸過程,然后 unlink() 之后刪除文件.
Use file_get_contents() to download the file to your server, then file_put_contents() to store it in a temporary, local file for the upload FB transfer process, then unlink() to delete the file afterwards.
<?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
}
這篇關于上傳遠程照片到上傳的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!