【问题标题】:Facebook api php sdk 4.0 - uploading photo to facebook using "new curlfile"Facebook api php sdk 4.0 - 使用“新 curlfile”将照片上传到 facebook
【发布时间】:2014-07-22 14:38:12
【问题描述】:

我正在尝试通过这个 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 上发布消息。

【问题讨论】:

    标签: php facebook session facebook-graph-api facebook-sdk-4.0


    【解决方案1】:

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

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

    【讨论】:

    • 你以前帮过我 :) 'source' => new CURLFile ($location.$name) 我做到了,感谢你 :)
    猜你喜欢
    • 2014-09-11
    • 2014-09-13
    • 1970-01-01
    • 1970-01-01
    • 2014-09-15
    • 1970-01-01
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多