Reputation: 29
How can i get 10 search result for google api, i have a code but it showing 4 search results only
$query = 'akon';
$url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=".$query;
$body = file_get_contents($url);
$json = json_decode($body);
for($x=0;$x<count($json->responseData->results);$x++){
echo "<b>Result ".($x+1)."</b>";
echo "<br>URL: ";
echo $json->responseData->results[$x]->url;
echo "<br>VisibleURL: ";
echo $json->responseData->results[$x]->visibleUrl;
echo "<br>Title: ";
echo $json->responseData->results[$x]->title;
echo "<br>Content: ";
echo $json->responseData->results[$x]->content;
echo "<br><br>";
}
Upvotes: 1
Views: 2142
Reputation: 1684
The maximum number of results that can be obtained from this api is 8. You can do that by adding "&rsz=large" to your url as below.
$url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&rsz=large&q=".$query;
To get more than 8 results, you need to perform the above operation twice by adding another parameter called "start" which is zero index based. So for the first page of results the $url must be,
$url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&rsz=large&start=0&q=".$query;
For the second page it will be,
$url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&rsz=large&start=8&q=".$query;
Upvotes: 1