Object-Oriented PHP - Concepts, Techniques, And Code

Page 127

OOPHP_02.book Page 107 Friday, May 5, 2006 2:25 PM

The doGoogleSearch method returns a GoogleSearchResult made up of the following elements: /* GoogleSearchResults are made up of documentFiltering, searchComments, estimatedTotalResultsCount, estimateIsExact, resultElements, searchQuery, startIndex, endIndex, searchTips, directoryCategories, searchTime */

Getting the Results We are only interested in three of the properties of the GoogleSearchResult: the time our search took, how many results are returned, and the results themselves. $searchtime = $results->searchTime; $total = $results->estimatedTotalResultsCount; if($total > 0){

The results are encapsulated in the resultElements property. //retrieve the array of result elements $re = $results->resultElements; ResultElements have the following characteristics: /* ResultElements are made up of summary, URL, snippet, title, cachedSize, relatedInformationPresent, hostName, directoryCategory, directoryTitle */

We iterate through the ResultElements returned and display the URL as a hyperlink along with the snippet of text that surrounds the search results. foreach ($re as $key => $value){ $strtemp = "<a href= \"$value->URL\"> ". " $value->URL</a> $value->snippet<br /><br />\n"; echo $strtemp; } echo "<hr style=\"border:1px dotted black\" />"; echo "<br />Search time: $searchtime seconds."; }else{ echo "<br /><br />Nothing found."; } }

K eepi n g I t F res h

107


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.