22

I need to get google search results for query. But using something like this

$query = 'Nikita Platonenko';
$url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=".urlencode($query);
$body = file_get_contents($url);
$json = json_decode($body);
var_dump($json)

i get only 4 results, I've already read about google ajax search but couldn't understand it. Please advise how to get all resulsts, or just first 100 results?

user1279525
  • 539
  • 1
  • 6
  • 12
  • The same question: [Google AJAX API - How do I get more than 4 Results?](http://stackoverflow.com/questions/4868815/google-ajax-api-how-do-i-get-more-than-4-results) – kenorb Nov 22 '14 at 20:38

2 Answers2

28
<?php
$query = 'Nikita%20Platonenko';
$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>";

}

?>

As the AJAX Api is now depreciated, you can use a third party service like SerpApi to get Google results. They have a GitHub repository, and it should be easy to integrate:

$query = [
    "q" => "Coffee",
    "google_domain" => "google.com",
];

$serp = new GoogleSearchResults();
$json_results = $serp.json($query);
Hartator
  • 5,029
  • 4
  • 43
  • 73
Monchito
  • 305
  • 3
  • 2
  • this a great example. can you tell its limitations. Thanks – rakibtg Jul 16 '13 at 14:34
  • 8
    @Monchito thanks for awesome answer i have little query it is giving only 4 results how can i get more result – M Gaidhane Jan 21 '14 at 09:22
  • 1
    to get more results fire another query to server with `start` parameter. like `http://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=$query&start=4` – shyammakwana.me Jan 03 '15 at 03:02
  • is this still working? couldnt get it to work - get NULL json. tks – tony gil Sep 04 '15 at 00:19
  • hi , it is working for me. i only need first link from seach result. but I want to know that it is free using this link($url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=".$query; ) or there is some restriction? – stackers Feb 15 '16 at 20:53
  • 2
    No longer working as of writing this. Returns: {"responseData": null, "responseDetails": "The Google Web Search API is no longer available. Please migrate to the Google Custom Search API (https://developers.google.com/custom-search/)", "responseStatus": 403} – slothstronaut Sep 15 '16 at 17:27
  • Not Working.it gives error – Visakh B Sujathan Jan 31 '18 at 10:35
  • The JSON API is newer and has not been deprecated. It can handle 10k requests daily. – Bangkokian Oct 11 '18 at 13:36
5

The answer:

Here is what I use successfully:

http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=[q]&key=[key]&cx=[account]&rsz=large&userip=[userip]&start=[start]

I don't think you have full control over how many results that can be obtained in the query. But you can control the results size (rsz=large), and where it starts.

Other notes:

In addition, it is always good to include the user's ip address there. Because, Google limits the number of queries based unique ip addresses (meaning, how many from an ip address). So if they are all coming from your server, you will be limited in how many queries you can send, but the limits go way down if you send the user's ip address. In addition, caching the results for a couple of days is an added bonus.

Community
  • 1
  • 1
Joseph at SwiftOtter
  • 4,276
  • 5
  • 37
  • 55