php Extract Best guess for this image result from google image search? php Extract Best guess for this image result from google image search? curl curl

php Extract Best guess for this image result from google image search?


You could use preg_match instead.

As you're getting the HTML back from CURL, you can then use Regex to match the text instead:

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);    }    $matches = array();    preg_match('/Best guess for this image:[^<]+<a[^>]+>([^<]+)/', $searched, $matches);    return (count($matches) > 1 ? $matches[1] : false);}


If you want to use DOMDocument you can get the values with the following modification.

    <?phpfunction 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);    if(true == ($topblock = $xml->getElementByID('topstuff')))    {        foreach($topblock->getElementsByTagName('div') as $div){            if(strstr(strtolower($div->nodeValue), "guess")){                foreach($div->getElementsByTagName('a') as $a){                    $last = $a->nodeValue;                }            }        }    }    return $last; }$content = fetch_google($_GET['img'],1);echo $content."<br>";?>