Google PHP 中的反向图像查找

Google reverse image lookup in PHP

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

尝试实施那里列出的方法,但似乎这些天,google 把你漂亮的 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 页面内容。这是该代码的 get 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;
}

非常感谢任何想法!

一个link到完整的API,可以用在PHP,就是:

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

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 更好的解决方案。也许 Google 应该买它们,然后它们就会 Google。哈哈