【问题标题】:Get Google Search Images using php使用 php 获取 Google 搜索图像
【发布时间】:2013-05-03 16:22:33
【问题描述】:

使用汽车关键字在 Google 图片上搜索并获取汽车图片。

我找到了两个这样实现的链接,

  1. PHP class to retrieve multiple images from Google using curl multi handler
  2. Google image API using cURL

也实现了,但它只提供了 4 张随机图像。

问题:如何使用我想要实现的关键字在 PHP 中获取汽车图像,就像我们在 Google 上搜索一样?

任何建议将不胜感激!!!

【问题讨论】:

  • 您在问题中发布的链接底部有一条说明,如果您想要超过 4 张图片,请尝试以下操作:webcodingeasy.com/PHP-classes/…
  • @shannonman 是的,当我打开此链接时,我的第一个链接打开并给出相同的结果,我也更改了 $count 值但得到相同。
  • @TonyStark 请看My Answer

标签: php curl google-image-search


【解决方案1】:

您可以为此使用PHP Simple HTML DOM library

<?php
    include "simple_html_dom.php";
    $search_query = "ENTER YOUR SEARCH QUERY HERE";
    $search_query = urlencode( $search_query );
    $html = file_get_html( "https://www.google.com/search?q=$search_query&tbm=isch" );
    $image_container = $html->find('div#rcnt', 0);
    $images = $image_container->find('img');
    $image_count = 10; //Enter the amount of images to be shown
    $i = 0;
    foreach($images as $image){
        if($i == $image_count) break;
        $i++;
        // DO with the image whatever you want here (the image element is '$image'):
        echo $image;
    }

这将打印特定数量的图像(数量在“$image_count”中设置)。

有关 PHP Simple HTML DOM 库的更多信息click here.

【讨论】:

  • 你在 foreach 循环中忘记了 $i++
【解决方案2】:

我对此不太确定,但谷歌仍然提供了一个很好的文档。

 $url = "https://ajax.googleapis.com/ajax/services/search/images?" .
   "v=1.0&q=barack%20obama&userip=INSERT-USER-IP";   
// sendRequest
// note how referer is set manually
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, /* Enter the URL of your site here */);
$body = curl_exec($ch);
curl_close($ch);

// now, process the JSON string
$json = json_decode($body);
// now have some fun with the results...

这是来自 Google 官方关于图像搜索的开发者指南。 如需更多参考,您可以在此处获得相同的参考。

 https://developers.google.com/image-search/v1/jsondevguide#json_snippets_php

在$url中你必须设置搜索关键字。

【讨论】:

  • Google 图片搜索 API 已于 2011 年 5 月 26 日正式弃用。
猜你喜欢
  • 1970-01-01
  • 2021-08-06
  • 2014-01-11
  • 1970-01-01
  • 2011-03-10
  • 2017-10-14
  • 1970-01-01
  • 1970-01-01
  • 2017-10-27
相关资源
最近更新 更多