Google Hacks - Looping around the 10-result Limit in PHP

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jase

    Google Hacks - Looping around the 10-result Limit in PHP

    I have put together the Google Web API with PHP, as described in
    O'Reilly's Google Hacks (Hack#55). However, the API only allows you to
    return 10 results. Hack #51 shows how to Loop around the 10-result
    limit but only describes how to do this in Perl. Anybody have this
    book or know how I can get around this.


  • Allodoxaphobia

    #2
    Re: Google Hacks - Looping around the 10-result Limit in PHP

    On 14 Mar 2004 13:57:16 -0800, jase hath writ:[color=blue]
    > I have put together the Google Web API with PHP, as described in
    > O'Reilly's Google Hacks (Hack#55). However, the API only allows you to
    > return 10 results. Hack #51 shows how to Loop around the 10-result
    > limit but only describes how to do this in Perl. Anybody have this
    > book or know how I can get around this.[/color]

    I have the book -- just can't locate it now. :-) I remember
    reading that section of the book and wondering "Why so much effort?"

    Here's is my bookmark for starting an "Advanced Google Search":



    Notice the num=50 , that does it for me in a browser.
    If you only specify "simple search terms", it proceeds
    v-a-v a "normal" Google search _and_ returns 50 (if there
    were that many) hits.

    HTH
    Jonesy
    --
    | Marvin L Jones | jonz | W3DHJ | OS/2
    | Gunnison, Colorado | @ | Jonesy | linux __
    | 7,703' -- 2,345m | config.com | DM68mn SK

    Comment

    • Hartmut König

      #3
      Re: Google Hacks - Looping around the 10-result Limit in PHP

      Hi Jase,

      jase wrote:[color=blue]
      > I have put together the Google Web API with PHP, as described in
      > O'Reilly's Google Hacks (Hack#55). However, the API only allows you to
      > return 10 results. Hack #51 shows how to Loop around the 10-result
      > limit but only describes how to do this in Perl. Anybody have this
      > book or know how I can get around this.[/color]

      Try this:

      $search_perpage = 10;

      //---------------------------------------------------------------//
      // Ask GOOGLE
      //---------------------------------------------------------------//
      $gs = new GoogleSearch();

      //set Google licensing key
      $gs->setKey("yourli censekey");

      //set max. number of results to be returned.
      $gs->setMaxResults( $search_perpage );

      $gs->setSafeSearch( true); //set Google "SafeSearch " feature.
      $gs->setFilter(true );
      $gs->setWSDLURL("Go ogleSearch.wsdl ");
      $gs->setQueryString ($query); //set query string to search.

      $search_runs = 5;
      $search_counter = 1;
      $count = 0;

      while($search_c ounter <= $search_runs)
      {
      $startindex=(($ search_counter* $search_perpage )-$search_perpage );

      $gs->setStartResult ($startindex);

      //call search method on GoogleSearch object
      $search_result = $gs->doSearch();

      //check for errors
      if(!$search_res ult)
      {
      if($err = $gs->getError())
      {
      echo "<br>Error: " . $err;
      exit("<br>Exiti ng...");
      }
      }

      $ranking[$query]["total"] =
      $search_result->getEstimatedTo talResultsCount ();

      //output individual components of each result
      $re = $search_result->getResultEleme nts();

      //-- Put the result in another data structure
      foreach($re as $element)
      {
      $count++;
      if(preg_match("/[\/|\.]$domain/",$element->getURL()))
      {
      $ranking[$query]["ranks"][$count]["title"]=
      $element->getTitle();
      $ranking[$query]["ranks"][$count]["url"] =
      $element->getURL();

      }

      $search_counter ++;
      }

      Hope this helps.

      Hartmut


      --
      SnakeLab - Internet und webbasierte Software | /\ /

      Hartmut König (mailto:h.koeni g@snakelab.de) | /\/ \ /

      ____________htt p://www.snakelab.de ________/\/\| /\/ \/

      Kennen Sie Ihre Shopkunden ? ShopStat schon ->\/_____________

      Comment

      Working...