douzhuozhu9544 2012-04-15 19:25
浏览 52
已采纳

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

Following is the code that works fine if I am passing /pageid/feed to facebook->api, but when I change it to upload the image directly to my Facebook photo album, it will not work. Kindly let me know what I am doing wrong in the following code:

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

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

Full Source

    <?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条回答 默认 最新

  • dpl22899 2012-04-15 20:26
    关注

    The path you are using /406221796071956/photos is wrong and that's why it does not work.

    The reason that this path is wrong, is that 406221796071956 is the id of a picture, see for yourself, the Graph API Explorer for 406221796071956 says:

    "type": "photo"

    If you want to publish to that album then use /196878530339618/photos, since it's the album: Graph API Explorer for 196878530339618

    "type": "album"


    Edit

    Please refer to https://developers.facebook.com/docs/reference/api/album/#photos for adding photos to an album.


    2nd Edit

    According to the documentation of the Album object:

    can_upload

    Determines whether the UID can upload to the album and returns true if the user owns the album, the album is not full, and the app can add photos to the album

    So you need the album not to be full, to be the owner of the album and have the right permissions for uploading.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上