2 /** @package verysimple::Search */
5 * include required files
7 require_once("SearchEngine.php");
8 require_once('SOAP/Client.php'); // PEAR::SOAP::Client
11 * This is an implmentation of a SearchRank/SearchEngine for Google.
13 * a convenient way to execute search queries, get results, ranking and inbound
17 * $google = new Google($API_KEY); // Google API Key must be obtained from Google
19 * $rank = $google->GetRank($url,$searchterm,$MAX_RESULTS);
20 * $links = $google->GetLinks($url);
22 * @package verysimple::Search
23 * @author VerySimple Inc.
24 * @copyright 1997-2007 VerySimple, Inc.
25 * @license http://www.gnu.org/licenses/lgpl.html LGPL
28 class Google
extends SearchEngine
32 * Returns the inbound links for the given url
35 * the url for which you want to find the rank
37 * the maximum number to return
38 * @return SearchResult
40 function GetLinks($url, $max = 10)
42 return $this->DoSearch("link:" . $url, 0, $max);
46 * Returns the estimated number of inbound links for the given url
49 * the url for which you want to find the rank
52 function GetLinkCount($url)
54 return $this->GetLinks($url, 1)->estimatedTotalResultsCount
;
58 * Given a url and result returned by DoSearch, returns the rank that url appears on the page
61 * the url for which you want to find the rank
62 * @param string $query
64 * @param int $maxresults
65 * (default 30) the max number of results to search for.
68 function GetRank($url, $query, $maxresults = 30)
74 $rank = new SearchRank();
76 $rank->Query
= $query;
78 while ($rank->Position
== 0 && $result_counter <= $maxresults) {
81 $result = $this->DoSearch($query, $result_counter, $page_size);
83 // only loop through as many results as we have
84 if ($result->estimatedTotalResultsCount
< $maxresults) {
85 $maxresults = $result->estimatedTotalResultsCount
;
88 if ($current_page == 1 && $result->estimatedTotalResultsCount
> 0) {
89 $rank->EstimatedResults
= $result->estimatedTotalResultsCount
;
90 $rank->TopRankedUrl
= $result->resultElements
[0]->URL
;
91 $rank->TopRankedSnippet
= $result->resultElements
[0]->snippet
;
92 $rank->TopRankedTitle
= $result->resultElements
[0]->title
;
95 $cp_rank = $this->GetPositionOnPage($url, $result);
97 if ($cp_rank->Position
) {
99 $rank->Page
= $current_page;
100 $rank->Position
= $cp_rank->Position
* $current_page;
101 $rank->Title
= $cp_rank->Title
;
102 $rank->Snippet
= $cp_rank->Snippet
;
103 $rank->Url
= $cp_rank->Url
;
106 $result_counter +
= $page_size;
114 * Given a url and result returned by DoSearch, returns the rank that url appears on the page
117 * @param Result $result
120 function GetPositionOnPage($url, &$result)
122 $rank = new SearchRank();
125 foreach ($result->resultElements
as $element) {
127 // print "<div>$url :: $counter = ".$element->URL."</div>";
128 $normalizedurl = str_replace("/", "\\/", $url);
130 if ($rank->Position
== 0 && preg_match("/$normalizedurl/i", $element->URL
)) {
131 $rank->Position
= ( int ) $counter;
132 $rank->Title
= $element->title
;
133 $rank->Snippet
= $element->snippet
;
134 $rank->Url
= $element->URL
;
142 * Queries google for the specified search query
144 * @param string $query
147 * @param bool $filter
148 * @param bool $restrict
155 function DoSearch($query, $start = 0, $max = 10, $filter = false, $restrict = "", $safe = false, $lr = "", $ie = "", $oe = "")
164 $result = $this->_googleSoap($query, $start, $max, $filter, $restrict, $safe, $lr, $ie, $oe);
166 } catch (exception
$ex) {
167 $this->FailedRequests ++
;
169 if (preg_match("/Daily limit/i", $ex->getMessage()) ||
preg_match("/Invalid/i", $ex->getMessage())) {
174 throw new Exception("Tried to contact Google API $counter times without success:" . $ex->getMessage());
183 * Makes SOAP request to google
185 private function _googleSoap($query, $start = 0, $max = 10, $filter = false, $restrict = "", $safe = false, $lr = "", $ie = "", $oe = "")
187 $wsdl = new SOAP_WSDL('http://api.google.com/GoogleSearch.wsdl');
188 $soapclient = $wsdl->getProxy();
190 if (get_class($soapclient) != "SOAP_Fault") {
191 $result = $soapclient->doGoogleSearch($this->Key
, $query, $start, $max, $filter, $restrict, $safe, $lr, $ie, $oe);
193 throw new Exception("SOAP_Fault: " . $soapclient->message
);
196 if (PEAR
::isError($result)) {
197 throw new Exception("PEAR Exception: " . $result->message
);