\n";
$r .= "
\n";
$r .= "\n";
return $r;
}
class GoogleSearch {
var $appID;
var $errorMessage;
var $page;
var $query;
var $recordsPerPage;
var $results;
var $safeSearch;
var $totalPages;
var $totalRecords;
function GoogleSearch($appID) {
$this->appID = $appID;
$this->errorMessage = "";
$this->page = 1;
$this->query = "";
$this->recordsPerPage = 10;
$this->safeSearch = true;
$this->totalPages = 0;
$this->totalRecords = 0;
}
function setPage($page) {
$this->page = ($page < 1) ? 1 : $page;
}
function setQuery($query) {
$this->query = $query;
}
function search_header($query) {
$r = "\n";
return $r;
}
function search_navagation($q) {
if ($this->totalRecords <= $this->recordsPerPage)
return "";
$query = urlencode($q);
$query = htmlspecialchars($query,ENT_NOQUOTES,"UTF-8");
$r = "";
if ($this->page > 1)
$r .= " previous page | ";
else
$r .= " previous page | ";
if ($this->totalRecords > ($this->page * $this->recordsPerPage))
$r .= " next page ";
else
$r .= " next page ";
$r .= "
\n";
return $r;
}
function search_results() {
$r = "\n";
foreach ($this->results as $item) {
$r .= "- " . $item['title'] . "
\n";
$r .= "- " . htmlspecialchars($item['snippet'],ENT_NOQUOTES,"UTF-8");
$r .= (strlen($item['snippet']) > 5 ? "
\n" : "");
$r .= "" . htmlspecialchars(str_replace("http://","",$item['url']),ENT_NOQUOTES,"UTF-8") . "\n";
$r .= " \n";
}
$r .= "
\n";
return $r;
}
function search() {
$this->errorMessage = "";
$this->totalPages = 0;
$this->totalRecords = 0;
$this->results = array();
$parameters = array(
'key' => $this->appID,
'q' => $this->query,
'start' => ($this->page - 1) * $this->recordsPerPage,
'maxResults' => 10,
'filter' => false,
'restrict' => '',
'safeSearch' => ($this->safeSearch ? true : false),
'lr' => '',
'ie' => 'UTF-8', # No longer used by google - ignored
'oe' => 'UTF-8' # No longer used by google - ignored
);
$soapClient = new soapclientx('http://api.google.com/search/beta2');
$retry_count = 0;
$soapResult = false;
while( !$soapResult && $retry_count < 3) { # Try request 3 times before failing
$soapResult = $soapClient->call('doGoogleSearch', $parameters, 'urn:GoogleSearch');
$retry_count++;
}
if ($soapClient->getError()) {
$this->errorMessage = $soapClient->getError();
$msg = "Search error:\n" . $this->errorMessage . "\n";
$msg .= "Query: " . $this->query . "\n";
$msg .= "Date: " . date('D M d Y h:i:s');
if (defined("MAILTO"))
@mail(MAILTO,"** Search ERROR ***", $msg);
return false;
}
$this->totalRecords = $soapResult['estimatedTotalResultsCount'];
$this->totalPages = ceil($this->totalRecords / $this->recordsPerPage);
if (($this->totalRecords > 0) && (is_array($soapResult['resultElements']))) {
foreach ($soapResult['resultElements'] as $item) {
$this->results[] = array (
'url' => $item['URL'],
'displayurl' => $item['URL'],
# Google doesn't have cache URL's, but provides other API's for fetching from Google cache.
# As a result, cacheurl will always be empty. For compatability with MSNSearch class, it's
# left in the result set.
'cacheurl' => isset($item['CacheUrl']) ? $item['CacheUrl'] : "",
'title' => isset($item['title']) && trim($item['title']) != '' ? $item['title'] : $item['URL'],
'snippet' => isset($item['snippet']) ? strip_tags($item['snippet']) : ""
);
}
}
return true;
}
}
?>