如何标记随机朋友上传图片

时间:2012-08-14 13:04:23

标签: facebook api tags

我想知道当我使用此代码调出图像时,我是如何标记随机朋友的 此代码会打开相册并上传图片。

如果有人在这里可以帮助我改变什么,并添加代码“

谢谢

<?php
    require 'facebook.php';
    $facebook = new Facebook(array(
        'appId'  => '',
        'secret' => '',
        'cookie' => true,
    ));

    $user = $facebook->getUser();
    if ($user) {
        $facebook->setFileUploadSupport(true);

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

        //Get album ID of the album you've just created
        $album_uid = $create_album['id'];

        //Upload a photo to album of ID...
        $photo_details = array(
            'message'=> 'Photo message'
        );
        $file='images/logo.jpg'; //Example image file
        $photo_details['image'] = '@' . realpath($file);

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

?>

1 个答案:

答案 0 :(得分:0)

根据documentation我向您添加了代码

<?php
define('COUNT_FRIENDS', '10');

require 'facebook.php';
$facebook = new Facebook(array(
    'appId'  => '',
    'secret' => '',
    'cookie' => true,
));

$user = $facebook->getUser();
if ($user) {
    $facebook->setFileUploadSupport(true);

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

    //Get album ID of the album you've just created
    $album_uid = $create_album['id'];

    //Upload a photo to album of ID...
    $photo_details = array(
        'message'=> 'Photo message'
    );
    $file='images/logo.jpg'; //Example image file
    $photo_details['image'] = '@' . realpath($file);

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

    /*
    *  Make tags on photo
    */
    $photo_id = $upload_photo['id'];

    /*
    * Get friends info from FB
    */
    $result = $fb_app->api('/me/friends');

    $friend_uids = array();
    if($result && $result['data']){
       foreach($result['data'] as $friend){
          $friend_uids[] = $friend['id'];
       }
    }

    /*
    * Choose random friends UID's
    */
    $rand_keys = array_rand($friend_uids, COUNT_FRIENDS);
    foreach($rand_keys as $key){
       $friends[] = $friend_uids[$key];
    }

    foreach($friends as $friend_uid){
        $tag_params = array(
            'to'       => $friend_uid,
            'tag_text' => 'Sample tag text',
            'x'        => 0,
            'y'        => 0
        );
        $result = $facebook->api('/' . $photo_id . '/tags', 'POST', $tag_params);
    }
}
?>

只需实现返回UID列表的函数“getRandomFriends()”。