0

Hi and I have been looking around and found that google has a few search api's but haven't yet found one that returns the results as a javascript/ajax array. Is it possible to at least get the google api to send the results to my website as a variable/array in javascript/ajax/php because can't see any reference to this.
PS. This is in the php section because all results will eventually end up in php. Also I tried the file_get_contents() function and curl on the google homepage but google knows it's a bot.

2
Contributors
5
Replies
7
Views
7 Years
Discussion Span
Last Post by cwarn23
0

From all the pages I clicked I didn't find that section. But does anybody know how to return 10 results instead of 4 ?

0

Any idea on how to implement that into the php script on the first link? I can't see how it fit's in but am trying to work it out.

0

I have solved it and will post some code soon.

Edit:
Here is the code which solved my problem:

$searchterm='Paris Hilton';
$searchterm=urlencode($searchterm);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q='.$searchterm.'&start=0');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, 'http://www.syntax.cwarn23.info/');
$body = curl_exec($ch);
curl_close($ch);
$string = json_decode($body);
$dstring='';
foreach ($string->responseData->results AS $val) {
$dstring.='<<---<<>-><';
$dstring.=str_replace(array('<<---<<>-><','<b>','</b>','<em>','</em>','<i>','</i>'),'',$val->title);
$dstring.='<<---<<>-><';
$dstring.=str_replace('<<---<<>-><','',$val->url);
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q='.$searchterm.'&start=4');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, 'http://www.syntax.cwarn23.info/');
$bodyb = curl_exec($ch);
curl_close($ch);
$stringb = json_decode($bodyb);
foreach ($stringb->responseData->results AS $valb) {
$dstring.='<<---<<>-><';
$dstring.=str_replace(array('<<---<<>-><','<b>','</b>','<em>','</em>','<i>','</i>'),'',$valb->title);
$dstring.='<<---<<>-><';
$dstring.=str_replace('<<---<<>-><','',$valb->url);
}
$dstring=substr($dstring,11);
//echo '<br>'.$dstring;
$array=explode ('<<---<<>-><',$dstring);
echo '<xmp>';
print_r($array);
echo '</xmp>';

Edited by cwarn23: added code

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.