«

»

Mar 21

Get Google Search Results with PHP

Want to get all the top google  search results in an array with PHP? Just read on and do it yourself!

This script basically outputs the url’s of all the top results of a google search, but it can be modified to output other details also.
We will be using the google ajax api in this script. As you might have found, by default it gives out only 4 results. Or you may manage to get 8 with one more parameter. But here, with some tweaks, you will get the top 64 results! (And 64 is the upper limit, because google doesn’t like bots.)

Looking for a reliable google search results parsing/scraping solution? Head over to our true Google Search API now! It is updated regularly and has a lot more features, and works perfectly even after the change in google’s HTML structure on 21 November, 2012. Requires just one function call to get the results in a nicely formatted associative array.

Here is what we have done in this script:

  • First, we have a function that requests search results from google.
  • google_search_api($args, $referer = ‘http://localhost/testing/’, $endpoint = ‘web’)
    We have used the usual code provided by google to fetch the search results with php.
  • But have done a tweak to get 8 results at a time, instead of 4. the tweak is:
    $args['rsz']=’8′;
    This code tells google to return 8(the maximum for one query) results instead of the default 4.
  • The next step is to get all the 64 results for a single search. We go around with it by querying again and again and specifying the “start” parameter in the search request url.
    $res = google_search_api(array(‘q’ => $query,‘start’=>$rez['responseData']['cursor']['pages'][$i]['start']));
  • But before we request paged results, we need to setup a loop.
    $res = google_search_api(array(‘q’ => $query));
    $pages=$res['responseData']['cursor']['pages'];
    $nres=0;
    for($i=0;$i<count($pages);$i++)
    {  … }
    We do a request from google to get the values of start parameter. We get the whole pagination information in the array, $pages
  • The rest is just displaying the url’s of all the search results and the number of results returned.
  • you can display description, title, cached url,etc from the
    $res['responseData']['results']
    array. For the structure of the results array, use
    print_r($res['responseData']['results']);

Hope you find this code helpful. Please feel free to comment and ask any doubt.

  • Sharif

    Wow It was really great for me . Thanks a lot 

  • Emad_eyl

    please help,i have this error… 
    Parse error: syntax error, unexpected T_IF in C:AppServwwwtestsearch4.php on line 6…

  • 2nickpick

     Is there anyway to modify this for location based search? All the results I obtain assume my location is set to “United States”. I’d like to be able to adjust that to my true location that Google sets when I visit the physical engine. I’ve done a little bit of research and looks like it might not be possible. Thoughts?

    • http://thetechnofreak.com/ technofreak

      yes there is. Use the code in my other post on 1000 results and replace google.com with goo.co.uk or what you want.

  • Santhanakumar

    Nice script.
    Is there any possible to get 100 records ?
    Thanks

    • http://physcs.com/ Yash Gupta

      Yes, it is possible to get any number of results , up to a maximum of 1000 with this script. Stay subscribed for another script which simplifies this stuff to just a single function call, more features, just a little patience required!

  • Oak

    Thank you very much
    I don’t understand most of the code (yet) but this is very helpful. :)

  • http://twitter.com/WebCodingNinja Web Coding Ninja

    It is not working it is just showing the first 8 right links then the next 8 are just repeated ?? any solution

  • Rajesh

    Can I convert this as a php class… so I can use like:
    $googlesearch = new google_search_api();
    $totalitems =  
    googlesearch ->args(5); 
    $googleget = $googlesearch->url; // for getting urls
    $googleget = $googlesearch->title; // for getting title

    I am working on a script, hence need to reuse this function… please help if you can modify to work around on this. BTW, I will be using this function inside framework like YII….

    Thanks for the wonderful post…

    Rajesh

    • http://thetechnofreak.com/ technofreak

      I have someting strikingly similar I coded few months back, but forgot to publish on my website using a function (instead of an object) to do everything on it’s own (including automatic proxy switching support) and return nicely formatted array of results and other details (like total results scraped, total found by google, block status, etc.)
      That script is available for commercial use if anyone want’s to buy, contact us for demo’s and other details etc.

    • http://physcs.com/ Yash Gupta
  • Kannan

    HI, The cod eworks fine :)
     But there is a small eror in the code

    on line 29    ‘start’=>$rez here its $res

    Thank you :)

  • semoho

    I have problem; when i did a few queries and google has blocked me
    how i can solve this problem?

  • Victor3434

    this is awesome,thank you much!!!

  • jonwolf

    with any search, there is no result for me. it’s seem to not work with the new google image
    any1 have solution ?