Facebook API适用于/ feed,但不适用于/ photos

时间:2012-04-15 19:25:11

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

如果我将/pageid/feed传递给facebook->api,以下代码可以正常工作,但是当我更改它以将图像直接上传到我的Facebook相册时,它将无效。请在下面的代码中告诉我我做错了什么:

// Works fine
$status = $facebook->api("/194458563914948/feed", 'post', $attachment);

// Does not work
$status = $facebook->api("/196878530339618/photos", 'post', $attachment);

完整来源

    <?php

require 'src/facebook.php';

$app_id = "332267477347";
$app_secret = "xxxx";

$facebook = new Facebook(array(
 'appId' => $app_id,
 'secret' => $app_secret,
 'cookie' => true,
 'fileUpload' => true,

));

$facebook->setFileUploadSupport(true);  


$user = $facebook->getUser();
//echo $user;

if(($facebook->getUser())==0)
{
 header("Location:{$facebook->getLoginUrl(array('scope' => 'photo_upload,user_status,publish_stream,user_photos,manage_pages'))}");
 exit;
}
else {
$accounts_list = $facebook->api('/me/accounts');
echo "i am connected";
}
  $valid_files = array('image/jpeg', 'image/png', 'image/gif');


//to get the page access token to post as a page
foreach($accounts_list['data'] as $account){
      if($account['id'] == 194458563914948){      // my page id =123456789
        $access_token = $account['access_token'];
        echo "<p>Page -- Access Token: $access_token</p>";
        }
    }

//posting to the page wall

if (isset($_FILES) && !empty($_FILES))
{  
$aid = '421539304540205';
$folder = "pak/".$_FILES['pic']['name'];
$fold = 'http://snowdrop.com.pk/fb/'.$folder;
echo $fold."<br>";
if( move_uploaded_file($_FILES['pic']['tmp_name'], $folder) )
{
#Upload photo here
  $img = realpath($_FILES["pic"]["tmp_name"]);
$attachment = array('message' => $_POST['textfield'],
                           'aid' => $aid,
   'source' => '@' . $img,
                                'access_token' => $access_token,


                );
$status = $facebook->api("/421539304540205/photos", 'post', $attachment);

echo $status;
var_dump($status);

 }
 else{
    echo 'Only jpg, png and gif image types are supported!';

}
}
?>
<body>
 <!-- Form for uploading the photo -->
 <div class="main">
  <p>Select a photo to upload on Facebook Fan Page</p>
  <form method="post" action="" enctype="multipart/form-data">
  <p>Select the image: <input type="file" name="pic" />
    <br />
    <label>Description
    <input type="text" name="textfield" id="textfield" />
    </label>
  </p>
  <p><input class="post_but" type="submit" value="Upload to my album" /></p>
  </form>
 </div>
</body>

1 个答案:

答案 0 :(得分:4)

您使用的路径 / 406221796071956 / photos 是错误的,这就是它无效的原因。

此路径错误的原因是 406221796071956 是图片的ID,请亲自看看,Graph API Explorer for 406221796071956说:

  

“type”:“photo”

如果您要发布到该相册,请使用 / 196878530339618 / photos ,因为它是相册:Graph API Explorer for 196878530339618

  

“type”:“album”


修改

请参阅https://developers.facebook.com/docs/reference/api/album/#photos将照片添加到相册中。


第二次编辑

根据Album object的文档:

  

<强> can_upload

     

确定UID是否可以上传到相册,如果用户拥有相册,相册未满,则返回true,   该应用可以将照片添加到相册

所以你需要专辑不是完整的,要成为专辑的所有者并拥有正确的上传权限。