问题描述
我在使用 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 (将#修改为@)