开发者

How can I get one photo for every Flickr tag by a specific user in one request?

开发者 https://www.devze.com 2023-03-18 07:40 出处:网络
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\

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 = u开发者_如何学Gonserialize(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>';

?>


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.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消