I am trying to scrape Google search results using PHP.
I tried using @file_get_contents(http://www.google.com/search?hl=en&q=test) but it does not work. It only works with http://www.google.com.
I tried using curl instead. Here's my function:
function my_fetch($url,$user_agent='Mozilla/4.0 (compatible; MSIE
5.01; Windows NT 5.0)') {
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt ($ch, CURLOPT_HEADER, 0);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_REFERER, 'http://www.google.com/');
$result = curl_exec ($ch);
curl_close ($ch);
return $result; }
$googleContent = my_fetch("http://www.google.com/search?hl=en&q=test");
echo $googleContent;
The result is
302 Moved
The document has moved here.
With a link to here: http://www.google.com/sorry/?continue=http://www.google.com/search%3Fhl%3Den%26q%3Dtest
Is there any way to crawl the search results using PHP without having to learn the API?