【问题标题】:Google reverse image lookup in PHP谷歌在 PHP 中的反向图像查找
【发布时间】:2015-01-23 04:15:07
【问题描述】:

发现自己需要能够通过反向图像查找来查询谷歌,以了解有关我的服务器上包含未知内容的图像的更多信息。在这里找到了一个很好的问题:php Extract Best guess for this image result from google image search?

尝试实现那里列出的方法,但似乎这些天来,谷歌获取您的漂亮 URL 并执行 302 重定向到看似随机生成的无意义 URL,将您带到图像搜索结果。我确保我的代码将 CURLOPT_FOLLOWLOCATION 设置为 1 以跟随,但我仍然取回 302 页面的内容。这是代码:

function fetch_google($terms="sample     search",$numpages=1,$user_agent='Mozilla/5.0 (Windows NT 6.1; rv:8.0) Gecko/20100101 Firefox/8.0')
{
    $searched="";
    for($i=0;$i<=$numpages;$i++)
    {
        $ch = curl_init();
        $url="http://www.google.com/searchbyimage?hl=en&image_url=".urlencode($terms);
        curl_setopt ($ch, CURLOPT_URL, $url);
        curl_setopt ($ch, CURLOPT_USERAGENT, $user_agent);
        curl_setopt ($ch, CURLOPT_HEADER, 0);
        curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt ($ch, CURLOPT_REFERER, 'http://www.google.com/');
        curl_setopt ($ch,CURLOPT_CONNECTTIMEOUT,120);
        curl_setopt ($ch,CURLOPT_TIMEOUT,120);
        curl_setopt ($ch,CURLOPT_MAXREDIRS,10);
        curl_setopt ($ch,CURLOPT_COOKIEFILE,"cookie.txt");
        curl_setopt ($ch,CURLOPT_COOKIEJAR,"cookie.txt");
        $searched=$searched.curl_exec ($ch);
        curl_close ($ch);
    }

    $xml = new DOMDocument();
    @$xml->loadHTML($searched);

    return $searched;
}

$content = fetch_google("http://upload.wikimedia.org/wikipedia/commons/thumb/0/0f/Grosser_Panda.JPG/1280px-Grosser_Panda.JPG",1);
echo $content."<br>";

还尝试了另一种实现来仅返回 URL,然后在返回的 URL 之后进行第二次 cURL 调用。同样的结果,返回 302 页面内容。这是该代码的获取 url 部分,该部分将给我一个 URL 以从中提取:

function get_furl($url)
{
$furl = false;

// First check response headers
$headers = get_headers($url);

// Test for 301 or 302
if(preg_match('/^HTTP\/\d\.\d\s+(301|302)/',$headers[0]))
{
    foreach($headers as $value)
    {
        if(substr(strtolower($value), 0, 9) == "location:")
        {
            $furl = trim(substr($value, 9, strlen($value)));
        }
    }
}
// Set final URL
$furl = ($furl) ? $furl : $url;

return $furl;
}

任何想法都非常感谢!

【问题讨论】:

  • Google 不希望您抓取他们的网络界面进行搜索,这可能就是您获得 302 的原因。如果您想自动查询 Google 的任何内容,您应该使用他们提供的 API 来完成为此。
  • 非常感谢。我阅读了 TOS 并理解了这个问题。
  • 您能否提供一个链接,指向 Google 自定义搜索 API 文档中处理后代反向图像搜索的部分?我在自定义搜索文档中,但找不到与按图像搜索相关的任何内容。 developers.google.com/custom-search/docs/…
  • @Kiko 谢谢你,你的回答很有帮助。但是,我找不到一个看起来可以用于反向图像搜索的参数。我仍然无法验证这是否真的可行。您是否在 API 的文档中发现了一些让您相信的东西?

标签: php curl google-image-search


【解决方案1】:

Tineye 有一个可用于反向图像搜索的 API。

http://services.tineye.com/TinEyeAPI

编辑:这是一个创建自己的图像搜索引擎的解决方案,用 python flask 编写。

https://github.com/realpython/flask-image-search http://www.pyimagesearch.com/2014/12/08/adding-web-interface-image-search-engine-flask/

我知道这与 Google 无关,但在这方面 Tineye 是比 Google 更好的解决方案。也许谷歌应该买下它们,然后它们就会成为谷歌。哈哈

【讨论】:

  • 鉴于谷歌没有提供合法的方法来做到这一点,使用替代方法就是答案。我一直在使用 Tinyeye,并且在使用 Imagga 时更加幸运:imagga.com 似乎在 2014 年出现,有一个非常漂亮的网站,他们会为您提供大量图片标签以及置信度对于我的机器人喜欢的标签。
【解决方案2】:

可以在 PHP 中使用的完整 API 的链接是:

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

代码示例为:

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

【讨论】:

  • 这是已弃用的 API。新的自定义搜索 API 似乎无法进行反向图像搜索。
  • 啊,是的,对不起。下次我应该读得更好。我找不到替代品。有足够多的搜索引擎 API,但没有一个可以让您使用图像进行搜索。我还注意到,除了用户搜索之外,谷歌不希望您将其引擎用于其他任何事情。我想简单的答案应该是你不能或可能不会做你想做的事。
猜你喜欢
  • 2022-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-29
相关资源
最近更新 更多