public function GoogleCSEServices::service in Google Custom Search Engine 8.3
Same name and namespace in other branches
- 8.2 src/GoogleCSEServices.php \Drupal\google_cse\GoogleCSEServices::service()
Sends query to Google's Custom Search Engine and returns response.
Parameters
string $keys: The search terms.
int $offset: The result number to start at.
Return value
string XML response string.
1 call to GoogleCSEServices::service()
- GoogleCSEServices::getAccurateResultsCount in src/
GoogleCSEServices.php - Get the exact (accurate) number of search results to be used in the pager.
File
- src/
GoogleCSEServices.php, line 121
Class
- GoogleCSEServices
- Additional functions as services for Google CSE.
Namespace
Drupal\google_cseCode
public function service($keys, $offset = 0) {
$page = 0;
$response = [];
if ($this->requestStack
->getCurrentRequest()->query
->has('page')) {
$page = $this->requestStack
->getCurrentRequest()->query
->get('page');
}
if (isset($response[$keys])) {
return $response[$keys];
}
// Number of results per page. 10 is the default for Google CSE.
// @TODO Confirm input in UI
$rows = (int) $this->CSEconfig
->get('configuration')['google_cse_adv_results_per_page'];
$query = [
'cx' => $this->CSEconfig
->get('configuration')['cx'],
'client' => 'google-csbe',
'output' => 'xml_no_dtd',
'filter' => '1',
'hl' => $this
->paramhl(),
'lr' => $this
->paramlr(),
'q' => $keys,
'num' => $rows,
'start' => $offset ? $offset : $page * $rows,
'as_sitesearch' => $this->CSEconfig
->get('configuration')['limit_domain'],
];
if ($this->requestStack
->getCurrentRequest()->query
->has('more')) {
$query['+more:'] = urlencode($this->requestStack
->getCurrentRequest()->query
->get('more'));
}
$url = Url::fromUri($this
->getProtocol() . 'www.google.com/cse', [
'query' => $query,
]);
// Get the google response.
$response = $this
->getResponse($url
->toString());
return $response;
}