为什么我无法获取脸谱相册ID?

时间:2012-01-09 11:46:10

标签: php facebook facebook-graph-api

我想要做的是在Facebook上创建相册并将照片上传到相册,现在它可以在Facebook上创建相册,但是上传照片到相册不起作用,我不知道为什么我无法获取相册ID ,任何解决方案或想法?

这是我的代码,即在facebook上创建相册和上传照片

<?//At the time of writing it is necessary to enable upload support in the Facebook SDK, you do this with the line:
    $facebook->setFileUploadSupport(true);

    //Create an album
    $album_details = array(
            'message'=> 'name',
            'name'=> 'name'
    );
    $create_album = $facebook->api('/me/albums', 'post', $album_details);

    //Get album ID of the album you've just created
    $albums = $facebook->api('/me/albums');
    foreach ($albums["data"] as $album) {
    //Test if the current album name is the one that has just been created
        if($album["name"] == 'name'){
            $album_uid = $album["id"];
        }
    }

    //Upload a photo to album of ID...
    $photo_details = array(
        'message'=> 'test'
    );
    $file='http://scm-l3.technorati.com/11/12/30/59287/google-logo-682-571408a.jpg'; //Example image file
    $photo_details['image'] = '@' . realpath($file);

    $upload_photo = $facebook->api('/'.$album_uid.'/photos', 'post', $photo_details);
    //print_r($upload_photo);
    ?>

感谢

2 个答案:

答案 0 :(得分:1)

我不认为您可以从网址上传文件,请尝试:


$pathToFile = "/some/path/someimage.jpg";
$facebook->setFileUploadSupport(true);
............
$create_album = $facebook->api('/me/albums', 'post', $album_details);
//get the album id
$album_uid = $create_album["id"]; // no need for foreach loop

.....
$args = array('message' => 'Some message');
$args['image'] = '@' . realpath($pathToFile);  //see realpath use here

$data = $facebook->api('/'. $album_uid . '/photos', 'post', $args);


希望有所帮助

答案 1 :(得分:0)

您无法针对http文件调用realpath。它仅适用于本地文件。 首先使用curlwget或其中一个php内部函数(如file_get_contents / file_put_contents)下载文件。

例如:

$file='http://scm-l3.technorati.com/11/12/30/59287/google-logo-682-571408a.jpg'; //Example image file
$output = tempnam(sys_get_temp_dir(), 'blex_');
$cmd = "wget -q \"$file\" -O $output";
exec($cmd);
$photo_details['image'] = '@' . realpath($output);