Facebook api php sdk 4.0-使用“上传照片到Facebook”;“新卷发文件”;

Facebook api php sdk 4.0-使用“上传照片到Facebook”;“新卷发文件”;,php,facebook,session,facebook-graph-api,facebook-sdk-4.0,Php,Facebook,Session,Facebook Graph Api,Facebook Sdk 4.0,我正试图通过这个facebook应用程序将我保存在服务器上的照片上传到我的facebook上 我的facebook请求最初是: $response = (new FacebookRequest( $session, 'POST', '/me/photos', array( 'source' => '@' . $_FILES['file']['tmp_name'] , ) ))->execute()->getGraphOb

我正试图通过这个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.';
}
if(isset($\u POST['submit'])){
$file\u type=$\u FILES['file']['type'];//返回文件类型
$allowed=array(“image/jpeg”、“image/gif”、“image/png”、“image/jpg”);//指定允许的文件类型
如果(!in_数组($file_type,$allowed)){
$error_message='只允许使用jpeg、jpg、gif和png文件。
请单击“上一步”,然后重试。“; 回显$error_消息; 退出(); } $name=$_FILES['file']['name'];//上载文件的原始路径 $temp_name=$_FILES['file']['tmp_name'];//包含驻留在服务器上的临时文件的路径 如果(isset($名称)){ 如果(!empty($name)){ $location='uploads/';//将照片保存到文件夹:uploads 如果(移动上传的文件($temp\u name,$location.$name)){ echo“照片已成功上传”; } } } 否则{ echo“照片上载失败,请单击“上一步”再试一次。”; }

我知道该会话可以工作,因为我可以在facebook上发布消息。

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

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

你以前帮过我:)“source”=>new CURLFile($location.$name)我做到了这一点,多亏了你:)