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

时光总嘲笑我的痴心妄想 提交于 2020-01-02 10:23:47

问题


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>';

?>

回答1:


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).



来源:https://stackoverflow.com/questions/6603055/how-can-i-get-one-photo-for-every-flickr-tag-by-a-specific-user-in-one-request

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!