使用多个标签时,Flickr API会返回不一致的照片数量

时间:2010-08-22 21:34:31

标签: php api flickr

我正在尝试将我的Flickr帐户用作图片库的“主机”。我用一个共同标签'golftournament'标记了251张照片,每张照片都标有年份和照片中的任何玩家。

因此,例如,三张随机照片可能包含以下标签:

golftournament dan steve 2005(dan和steve在2006年的这张照片中)

golftournament 2006(2006年这张照片中没有玩家)

golftournament 2008 paul dan(2008年这张照片中的保罗和丹)

当我进行API调用时,如果我将API调用的“标记”部分设置为tags=golftournament,dan,2005且将tag_mode设置为tagmode=all,则会返回不一致的照片总数。

有时我会在结果中得到13张照片,有时候会拍到12张照片,有时我会得到正确数量的照片(14)

我正在使用PHP库,但这无关紧要,因为我在Flickr API Explorer中看到了相同的结果:http://www.flickr.com/services/api/explore/?method=flickr.photos.search

有没有理由说Flickr API在这方面如此不一致?

干杯,

2 个答案:

答案 0 :(得分:0)

为了更新这一点,最后我只是从照片集中获得了所有照片,然后根据标签构建了两个数组和玩家。最后,我使用了要求返回正确照片的内容。我正在使用优秀的phpFlickr library,这是我的整个API脚本:

<?php
// Initialise Flickr API library and authentication
require_once("phpFlickr.php");
$f = new phpFlickr('<api key>', '<secret>');
$f->setToken('<token>');

// Get all photos from the photoset
$page = 0;
$perpage = 500;
$all = array('photos' => array(), 'total' => 0);
while ($perpage * $page < $all['total'] || $all['total'] == 0) {
    $p = $f->photosets_getPhotos('<photoset number>', 'tags', NULL, $perpage, $page + 1);
    $all['total'] = (integer) $p['photoset']['total'];
    $all['photos'] += $p['photoset']['photo'];
    $page++;
}

// Get all the available years/players from the tags
$years = array();
$players = array();
foreach ($all['photos'] as $key => $photo) {
    $photo_tags = explode(' ', $photo['tags']);
    $all['photos'][$key]['tags'] = $photo_tags;
    foreach ($photo_tags as $tag) {
        if (preg_match('/^[0-9]{4}$/', $tag)) {
            if (!in_array($tag, $years)) {
                $years[] = $tag;
            }
        } else {
            if (!in_array($tag, $players)) {
                $players[] = $tag;
            }
        }
    }
}
rsort($years);
sort($players);

// Set year/player tags if set
$tags = array();
if (isset($_GET['year']) && in_array($_GET['year'], $years)) {
    $tags['year'] = $_GET['year'];
}
if (isset($_GET['player']) && in_array($_GET['player'], $players)) {
    $tags['player'] = $_GET['player'];
}

// Build output array and filter by year/person
$output = array(
    'years'     => $years,
    'players'   => $players,
    'photos'    => array()
);
foreach ($all['photos'] as $key => $photo) {
    if (!isset($tags['year']) || in_array($tags['year'], $photo['tags'])) {
        if (!isset($tags['player']) || in_array($tags['player'], $photo['tags'])) {
            $output['photos'][] = array(
                'thumbnail' => $f->buildPhotoURL($photo, 'Square'),
                'image'     => $f->buildPhotoURL($photo, 'Large'),
                'tags'      => implode(' ', $photo['tags'])
            );
        }
    }
}
$output['totals']['total'] = count($output['photos']);

// Calculate paging
$output['perpages'] = array(32, 64, 128, 256);
if (isset($_GET['perpage']) && in_array($_GET['perpage'], $output['perpages'])) {
    $output['perpage'] = $_GET['perpage'];
} else {
    $output['perpage'] = $output['perpages'][0];
}
if ($output['totals']['total'] <= $output['perpage']) {
    $output['pages'] = 1;
} else {
    $output['pages'] = ceil($output['totals']['total'] / $output['perpage']);
}
if (isset($_GET['page']) && intval($_GET['page']) > 0 && intval($_GET['page']) <= $output['pages']) {
    $page = $_GET['page'];
} else {
    $page = 1;
}
$output['totals']['recordstart'] = (($page == 1) ? 0 : (($page - 1) * $output['perpage']));
$output['totals']['recordend'] = ($output['totals']['recordstart'] + $output['perpage']) - 1;
if ($output['totals']['recordend'] > ($output['totals']['total'] - 1)) {
    $output['totals']['recordend'] = $output['totals']['total'] - 1;
}
$photos = array();
foreach ($output['photos'] as $index => $photo) {
    if ($index >= $output['totals']['recordstart'] && $index <= $output['totals']['recordend']) {
        $photos[] = $photo;
    }
}
unset($output['photos']);
$output['photos'] = $photos;

ob_start("ob_gzhandler");
echo json_encode($output);

希望有人觉得这很有用!

答案 1 :(得分:0)

显然flickr搜索功能是bugged at the moment,flickr正在修复。