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

发布于 2024-11-18 19:31:05 字数 2100 浏览 1 评论 0原文

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

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

?>

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

?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

水水月牙 2024-11-25 19:31:05

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

(这仅满足您对照片少于 500 张的用户的“一次请求”标准)。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文