You are here

function google_cse_adv_get_accurate_num_of_results in Google Custom Search Engine 7.2

Get the exact (accurate) number of search results to be used in the pager.

Google will never return more than 1000 results for any given search. If a request for the maximum results is made, Google will return the last page of the search results with the start and end position as attributes of the results.

The <RES> tag encapsulates the set of individual search results and details about those results. The tag attributes are SN (the 1-based index of the first search result returned in this result set) and EN (the 1-based index of the last search result).

Parameters

string $keys: The search keys.

$total: The initial estimated total.

Return value

int The accurate total number of results.

1 call to google_cse_adv_get_accurate_num_of_results()
google_cse_adv_response_results in google_cse_adv/google_cse_adv.inc
Function to fetch the results xml from Google.

File

google_cse_adv/google_cse_adv.inc, line 324
Uses Google Custom Search Engine (CSE) without frames and ads.

Code

function google_cse_adv_get_accurate_num_of_results($keys, $total) {
  $total_num_results = 0;

  // Allow other modules to alter the keys.
  drupal_alter('google_cse_searched_keys', $keys);
  $offset = GOOGLE_MAX_SEARCH_RESULTS - variable_get('google_cse_adv_results_per_page', 10);
  $response = google_cse_adv_service($keys, $offset);
  $xml = simplexml_load_string($response[0]);
  if (isset($xml->RES)) {

    // Get the 1-based index of the last search result item from the result end
    // attribute (EN) of the search result tag (RES).
    $attributes = $xml->RES
      ->attributes();
    $total_num_results += (int) $attributes['EN'];
  }

  // If we do not find an accurate result we will use the initial estimate.
  if (!$total_num_results) {
    $total_num_results = $total;
  }
  return $total_num_results;
}