【问题标题】:Cycle through images循环浏览图像
【发布时间】:2011-05-13 21:43:28
【问题描述】:

我正在使用 google images api 来检索有关图像的一些信息。唯一的问题是对start 参数的理解。这是我的主要周期:

// $num - necessary number of images to find
function doGoogle($query, $num) 
{

    // Search images until we have need number
    $i = $k = $n = 0;
    while(count($resultStorage) < $num)
    {
        $base_url = 'http://ajax.googleapis.com/ajax/services/search/images?v=1.0';
        $url = $base_url;
        if ($i == 0)
            $url .= '&imgsz=large&q='.urlencode($query).'&start=00';
        else
            $url .= '&imgsz=large&q='.urlencode($query).'&start='.$k.$n;

        // Request the page from google's api
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_URL, $url);
        $json = curl_exec($curl);
        curl_close($curl);
        $data = json_decode($json);

        echo '<div style="clear: both"></div>Page: <b>' . $i . '</b><br />Url: <b>' . $url . '</b><br />';

        // Cycle through the list of result images
        foreach($data->responseData->results as $v)
        {
            // If we already have neccessary number of images
            if (count($resultStorage) == $num) break;

            // Show it up
            echo "<div style='float: left'><a href='{$v->unescapedUrl}'><img src='{$v->tbUrl}' /></a></div>";
        }
        $i++;
        $k++;
        if ($k == 9) { $k = 0; $n++; }
    }
};

如何使用nk 进行操作?因为有时它会返回相同的图片(循环)。

您可以在以下页面找到我的示例代码:http://95.134.128.173/image.uploader.php(只需单击“搜索”)。

【问题讨论】:

    标签: php json api


    【解决方案1】:

    你的意思是当你按 N 或 K 键时它会前进和后退。

    如果是这种情况,您将需要使用 JavaScript,如果您不介意使用 Alt + N 和 Alt + K,那么您可以在(已经存在?)html 按钮上使用 accesskey="n" 属性下一个和上一个。

    如果不是,那我不明白你的意思。

    【讨论】:

    • 我在帮你请不要用那种语气。
    猜你喜欢
    • 2012-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-27
    • 1970-01-01
    • 2019-01-07
    • 2013-01-07
    • 2018-11-16
    相关资源
    最近更新 更多