Google AJAX api

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
W0utR
Forum Newbie
Posts: 2
Joined: Tue Sep 14, 2010 8:32 am

Google AJAX api

Post by W0utR »

I'm using the Google AJAX api to search google from my site, now the code works, the only problem I got is that the estimate amount of results doesn't match with the normal google.

Code: Select all

<?php
$apiKey = "ABQIAAAAt4f08-H084X-lC85HC1crxTb-vLQlFZmc2N8bgWI8YDPp5FEVBSak5rhmI-6wfaR89QTNpCOxJ8ZXA";
$url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=woutr&key=".$apiKey."&userip=".$_SERVER['REMOTE_ADDR'];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, "http://localhost:8888");
$body = curl_exec($ch);
curl_close($ch);

echo $body;
?>
Now, under "estimatedResultCount" I get : "536" and when i search on the normal google, i get 37,000 results.

Am i still doing something wrong? Or is this just normal
windsortommy
Forum Newbie
Posts: 2
Joined: Mon Sep 13, 2010 12:03 pm

Re: Google AJAX api

Post by windsortommy »

Hi,
I reseached your topic for a while. It seems that this is a common issue for the Google API, see the following link to the google discussion group:

http://groups.google.com/group/google-a ... this+group

One of the thread points out that maybe this is the issue of local search, but even i get rid of the "http://localhost:8888", it still gives the same result.
See we have to wait until someone comes up the solution or Google fix the problem.

cheers!
:D
W0utR
Forum Newbie
Posts: 2
Joined: Tue Sep 14, 2010 8:32 am

Re: Google AJAX api

Post by W0utR »

Yeah, I already checked the discussion groups, but couldn't find the answer.

Right now i'm using this piece of code:

Code: Select all

function getBookMarks($url, $site) {
	$url="http://www.google.com/search?num=10&hl=en&safe=off&q=site:$site%20$url&btnG=Search";
	$useragent="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1";
	
	$ch = curl_init($url);
	//curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
	//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER  ,1);
	//curl_setopt($ch, CURLOPT_COOKIEJAR, "./cookie-jar.txt");
	//curl_setopt($ch, CURLOPT_COOKIEFILE, "./cookie-jar.txt");
	
	curl_setopt($ch, CURLOPT_HEADER, 1);
	curl_setopt($ch, CURLOPT_AUTOREFERER, 0);
	$data = curl_exec($ch);
	curl_close($ch);
	
	if(preg_match("/did not match any documents/i", $data)) { 
		return(0);
	}
	
	$spl = explode("<div id=resultStats>", $data);
	$spl2 = explode(" results", $spl[1]);
	$ret = trim($spl2[0]);
	
	if(strlen($ret)==0) {
		return(0);
	} else {
		return($ret);
	}
}
Bassicly scraping the total results from the website, but i figured it would be easier / faster when i used the AJAX api
Post Reply