Connecting Tech Pros Worldwide Forums | Help | Site Map

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

jase
Guest
 
Posts: n/a
#1: Jul 17 '05
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.

http://sundaystroll.com

Allodoxaphobia
Guest
 
Posts: n/a
#2: Jul 17 '05

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":

http://www.google.com/advanced_searc...=en&lr=lang_en

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
Hartmut König
Guest
 
Posts: n/a
#3: Jul 17 '05

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("yourlicensekey");

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

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

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

while($search_counter <= $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_result)
{
if($err = $gs->getError())
{
echo "<br>Error: " . $err;
exit("<br>Exiting...");
}
}

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

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

//-- 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.koenig@snakelab.de) | /\/ \ /

____________http://www.snakelab.de________/\/\| /\/ \/

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

Closed Thread