Search code examples
phpcurlsimple-html-dom

Google search results with php


I'm using the following php script to get search results from Google.

include("simple_html_dom.php");
include("random-user-agent.php");


$query = 'facebook';

$curl = curl_init();  
curl_setopt($curl, CURLOPT_URL, 'http://www.google.com/search?q='.$query.'');   
#curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);   
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($curl, CURLOPT_USERAGENT,random_user_agent());
$str = curl_exec($curl);   
curl_close($curl);   

$html= str_get_html($str);  

$i = 0;
foreach($html->find('li[class=g]') as $element) {
    foreach($element->find('h3') as $item) 
    {
        $title[$i] = ''.$item->plaintext.'' ;
    }
       $i++;
}
print_r($title);

When this script runs in a cronjob (with 5 sec sleep) I receive a warning from Google and have to fill in a captcha (obvious). I always thought that using curl and a random user agent can avoid this. What is the correct solution?


Solution

  • A better way to avoid captcha is to set a randomized sleep between 3-6 seconds per request.

    Best solution is to use proxies.