public function GoogleSiteSearch::getSearchResults in Google Site Search 7
Same name and namespace in other branches
- 6 includes/GoogleSiteSearch.inc \GoogleSiteSearch::GetSearchResults()
Get search results.
Parameters
int $page: The page to get results from.
Return value
array The search results.
Throws
Exception In cases when no results are returned from Google.
File
- includes/
GoogleSiteSearch.inc, line 285 - GSS module site search inc file.
Class
- GoogleSiteSearch
- Class for interaction with Google Site Search.
Code
public function getSearchResults($page = 0) {
// Set page.
$this->currentPage = $page;
// Calculate start position based on page.
$start_pos = $this->currentPage * $this->pageSize;
$params = array(
'query' => array(
'start' => $start_pos,
'num' => $this->pageSize,
'client' => 'google-csbe',
'output' => 'xml_no_dtd',
'cx' => $this->key,
'q' => $this->query,
),
);
// Add language parameter if is set.
if ($this->language) {
// hl: "interface language", also used to weight results.
$params['query']['hl'] = $this->language;
// lr: "language restrict", supposed to limit results to only the set language, defined with a "lang_" prefix.
$params['query']['lr'] = 'lang_' . $this->language;
}
// Prepare query parameters for URL assembly.
if (count($this->extraParams) > 0) {
$params['query'] = array_merge($params['query'], $this->extraParams);
}
else {
$extra_params_query = NULL;
}
// Get the search base set in the admin, default to Google.
$search_base_url = variable_get('gss_base_url', '');
$search_base_url = !empty($search_base_url) ? $search_base_url : GSS_BASE_URL;
// Fetch results from Google.
$url_response = drupal_http_request(url($search_base_url, $params));
if (isset($url_response->error)) {
return NULL;
}
$results = simplexml_load_string($url_response->data);
if ($results !== FALSE) {
if (!isset($results->RES->M)) {
// No results, return NULL.
return NULL;
}
// Save total results.
$this->approxTotalResults = intval(check_plain((string) $results->RES->M));
$this->totalResults = intval($results->Context->total_results);
// Store faceted items in order to use them later in the header.
$categories = array();
if (isset($results->Context->Facet->FacetItem)) {
foreach ($results->Context->Facet->FacetItem as $facet) {
$categories[] = array(
'label' => check_plain((string) $facet->label),
'anchor_text' => check_plain((string) $facet->anchor_text),
);
}
$this->categories = $categories;
}
// Init results array.
$arr = array();
// Loop results.
foreach ($results->RES->R as $result) {
// Init result array.
$item = array();
$item['title'] = filter_xss((string) $result->T, $this
->getAllowedTags());
$item['url'] = check_url((string) $result->UE);
$item['description'] = filter_xss((string) $result->S, $this
->getAllowedTags());
// Let's get the image thumbnail - present in
// PageMap->DataObject['type'] == cse_thumbnail.
if (isset($result->PageMap->DataObject)) {
foreach ($result->PageMap->DataObject as $do) {
switch ((string) $do['type']) {
case 'cse_thumbnail':
// We are inside the thumbnail node, lets get the src attribute.
foreach ($do->Attribute as $att) {
switch ((string) $att['name']) {
case 'src':
$item['thumbnail_url'] = check_url((string) $att['value']);
global $is_https;
if ($is_https) {
$item['thumbnail_url'] = preg_replace('/^http:\\/\\//', 'https://', $item['thumbnail_url']);
}
break;
}
}
break;
}
}
}
$arr[] = $item;
}
// Return results.
return $arr;
}
throw new Exception('Could not load search results from Google.');
}