PHP中的Google反向图像查找

时间:2015-01-23 04:15:08

标签: php curl google-image-search

发现自己需要能够通过反向图像查询来查询谷歌,以了解有关我的服务器上具有未知内容的图像的更多信息。在这里找到了一个很好的问题: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;
}

任何想法都非常赞赏!

2 个答案:

答案 0 :(得分:2)

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/

我知道这与谷歌没有任何关系,但在这方面,Tineye比谷歌更好的解决方案。也许谷歌应该购买它们,然后他们将成为谷歌。哈哈

答案 1 :(得分:0)

可以在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...