微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Google 自定义搜索 api - 过滤图像版本

如何解决Google 自定义搜索 api - 过滤图像版本

我在使用 google-api-PHP-client 进行图像搜索(下面的代码)时,从 Google 自定义搜索 API 返回的结果出现问题。结果返回同一图像的许多版本,而不是实际相同的图像(例如,相同大小,因此“过滤器”=>“1”不是此处的解决方案),而是由搜索网站托管的图像版本。例如,当不同类型的客户端(桌面、移动等)检索它们所在的页面时使用的不同大小的版本。

我不认为这会是一个重大问题,但在测试中似乎几乎 80% 的结果都属于这一类。我下面的示例检索了 50 个图像 - 只有 11 个唯一图像/非版本。 Google 上的图像结果将这些过滤掉了 - api 中没有任何内容可以做到这一点吗?

<?PHP

//require the google-api-PHP-client
require_once 'resources/google-api-PHP-client/src/Google/autoload.PHP';

//initiate google client,set name and developer key
$client = new Google_Client();
$client->setApplicationName("test");
$client->setDeveloperKey("apikey");

//create an empty result array
$resultsarray = [];

//start on page one
$startoffset = 1;

//initiate custom search
$service = new Google_Service_Customsearch($client);

//loop while startoffset is less than or equal to 10
for ($startoffset = 1; $startoffset <= 5; $startoffset++)
{
    //search parametres (cx: name of search engine ID,searchtype: what type of search to perform,//num: number of results to return,start: what page of results to return
    $params = array("cx" => "searchengineid","searchType" => "image","filter" => "1","start" => $startoffset);

    //send search query 
    $results = $service->cse->listCse("cat",$params);

    //to test
    //echo "<pre>"; var_dump($results); echo "</pre>";

    //only get result items and loop through them
    foreach($results->getItems() as $k=>$item)
    {
        //retrieve title 
        $title = $item["htmlTitle"];

        //retrieve link to image (full size),image width,height,link to thumb,width and height
        $link = $item["link"];
        $fileformat = $item["fileFormat"];
        $width = $item["image"]["width"];
        $height = $item["image"]["height"];
        $thumblink = $item["image"]["thumbnailLink"];
        $thumbwidth = $item["image"]["thumbnailWidth"];
        $thumbheight = $item["image"]["thumbnailHeight"];

        //create temp array of search rusult details and add to results array
        $temparray = ["title" => $title,"link" => $link,"fileformat" => $fileformat,"width" => $width,"height" => $height,"thumblink" => $thumblink,"thumbwidth" => $thumbwidth,"thumbheight" => $thumbheight];
        $resultarray[] = $temparray;

    }
}

//to test - print result array in formatted output
echo "<pre>";
print_r($resultarray);
echo "</pre>";

?>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?