如何在一个请求中为特定用户的每个Flickr标签获取一张照片?

问题描述:

基本上我正在做的是生成缩略图页面.指定用户具有的每个标签都有一个缩略图.因此,如果用户使用了50个不同的标签,则将有50个缩略图(我将最终对其进行分页.).有用;这只是效率低下.即使只有8个标签,这也非常慢,因为它必须等待Flickr服务器的9个响应(标签列表为+1).有没有更有效的方法可以做到这一点?扫描Flickr API时似乎找不到更好的解决方案.以下是我目前正在执行的操作.

Basically what I am doing is generating a page of thumbnails. There is one thumbnail for each tag that the specified user has. So if the user has used 50 different tags there will be 50 thumbnails (I'll eventually paginate this.). It works; it's just inefficient. Even with just 8 tags, this is very slow since it has to wait for 9 responses (+1 for the list of tags) from the Flickr servers. Is there a more efficient way to do this? I can't seem to find a better solution whilst scanning the Flickr APIs. Below is what I am currently using to do this.

<?php   
    function get_api_url($additional_params) {
        $params = array_merge($additional_params, array(
            'api_key'   => API_KEY,
            'format'    => 'php_serial',
            'user_id'   => USER_ID,
        ));

        $encoded_params = array();
        foreach ($params as $k => $v)
            $encoded_params[] = urlencode($k) . '=' . urlencode($v);

        return 'http://api.flickr.com/services/rest/?' . implode('&', $encoded_params);
    }

    // Set any additional paramaters.
    $additional_params = array(
        'method'    => 'flickr.tags.getListUser',
    );

    // Get the tags.
    $rsp_obj = unserialize(file_get_contents(get_api_url($additional_params))); 

    // Parse the tags.
    $unparsed_tags = $rsp_obj['who']['tags']['tag'];
    $tags = array();
    foreach ($unparsed_tags as $tag) {
        $tags[] = $tag['_content'];
    }

    // Set any additional parameters.
    $additional_params = array(
        'method'    => 'flickr.photos.search',
        'per_page'  => '1',
    );
    $api_url = get_api_url($additional_params);

    // Call the API and parse the response.
    echo "<div id=\"tags\">";
    foreach ($tags as $tag) {
        $rsp_obj = unserialize(file_get_contents($api_url . '&tags=' . urlencode($tag)));
        $photo = $rsp_obj['photos']['photo'][0];

        $image_url = 'http://farm' . $photo['farm'] . '.static.flickr.com/' .
            $photo['server'] . '/' . $photo['id'] . '_' . $photo['secret'] . '_m.jpg';
        $tag_url = "/gallery/?tag=$tag";
        $tag = ucwords($tag);
        echo <<<HD
            <a class="tag" href="$tag_url">
                <img src="$image_url" />
                <span class="caption">$tag</span>
            </a>
HD;
    }
    echo '</div>';

?>

您可以使用flickr.people.getPhotos方法获取有关用户所有照片的信息(每页最多500张),并将extra=tags添加到用于电话.然后,您可以在内存中进行每个标签的选择.除非用户在帐户中每500张照片使用少于1个唯一标签,否则这将需要较少的API调用,这是以更大的API响应以及脚本中更多的内存使用和计算为代价的.

You could use the flickr.people.getPhotos method to get information about all of a user's photos (maximum 500 per page), and add extra=tags to the parameters for the call. Then you can do the per-tag selection in memory. This will require fewer API calls unless the user uses less than 1 unique tag per 500 photos in their account, at the cost of larger API responses and more memory use and computation within your script.

(对于少于500张照片的用户,这仅满足您的一个请求"条件.)

(This only meets your "one request" criteria for users with fewer than 500 photos).