1

我正在尝试通过此 facebook 应用程序将保存在服务器上的照片上传到我的 facebook。

我的 Facebook 请求最初是:

$response = (new FacebookRequest(
    $session,
    'POST',
    '/me/photos',
    array(
        'source' =>  '@' . $_FILES['file']['tmp_name'] ,
    )
))->execute()->getGraphObject()->asArray();

/* handle the result */
print_r($response);

但显然语法现在已经改变,我需要这样的东西:

'source'=> new CURLFile(' '),

由于我的图像存储在服务器的文件夹中,因此不确定要放置什么参数。
我的应用程序允许用户从他们的计算机浏览和提交照片。这将在发布到 Facebook 之前保存到服务器上。我使用了多种形式/部分:

<form method="post" enctype="multipart/form-data">
    Please select a photo to upload <input type="file" name="file" id="file"><br><br>
    <input type="submit" value="Upload" name="submit">
</form>

我必须将照片上传到服务器的代码如下:

if(isset($_POST['submit'])) {
    $file_type = $_FILES['file']['type']; //returns the file type
    $allowed = array("image/jpeg", "image/gif", "image/png", "image/jpg"); //specifies allowed file types
    if(!in_array($file_type, $allowed)) {
        $error_message = 'Only jpeg, jpg, gif, and png files are allowed. <br> 
        Please click back and try again.';
        echo $error_message;
        exit();
    }

$name = $_FILES['file']['name'];  //original path of the uploaded file
$temp_name = $_FILES['file']['tmp_name'];  //contains the path to the temporary file that resides on the server

if(isset($name)) {
    if(!empty($name)) {      
        $location = 'uploads/'; //save photo to the folder: uploads
        if(move_uploaded_file($temp_name, $location.$name)){
            echo 'Photo was successfully uploaded.';
        }
    }       
} 
else {
    echo 'Photo was unsuccessfully uploaded, click back and try again.';
}

我知道会话有效,因为我能够在 facebook 上发布消息。

4

1 回答 1

2

CURLFILE 方法采用一个主要参数,即路径和文件名。修改您的代码如下:

$response = (new FacebookRequest(
    $session,
    'POST',
    '/me/photos',
    array(
        'source' =>  new CURLFile( $_FILES['file']['tmp_name'] )
    )
))->execute()->getGraphObject()->asArray();
于 2014-08-14T10:07:38.503 回答