function googleSearch(\GoogleSearch &$google, $offset = 0, $userTotal = 200) { try { $sites = array(); $total = 0; do { $result = $google->doGoogleSearch(); $google->setStart($offset); if ($result->responseData->cursor->estimatedResultCount) { $total = $result->responseData->cursor->estimatedResultCount - $offset; if ($userTotal == 0) { $userTotal = $total; } } foreach ($result->responseData->results as $searchResult) { $url = $searchResult->visibleUrl; if (!in_array($url, $sites)) { $sites[] = $url; } } $offset += 8; } while ($offset < $total && $offset < $userTotal); } catch (Exception $e) { $google->log($e->getMessage(), 1, "red"); return $sites; } return $sites; }
/** * google search * @access public * @param string $term */ function googleSearch($term, $results) { global $_ARRAYLANG; /* * Example to access Google cached pages through GoogleSearch for PHP. */ $objGoogleSearch = new \GoogleSearch(); //set Google licensing key $key = $this->settings['google']['googleId']; $objGoogleSearch->setKey($key); //set query string to search. $objGoogleSearch->setQueryString($term); //set query string to search. //set few other parameters (optional) $objGoogleSearch->setMaxResults($results); //set max. number of results to be returned. $objGoogleSearch->setSafeSearch(true); //set Google "SafeSearch" feature. //call search method on GoogleSearch object $search_result = $objGoogleSearch->doSearch(); //check for errors if (!$search_result) { $err = $objGoogleSearch->getError(); if ($err) { \Cx\Core\Csrf\Controller\Csrf::header("Location: " . CONTREXX_SCRIPT_PATH . "?section=Directory&cmd=search"); exit; } } //output individual components of each result $re = $search_result->getResultElements(); if (!empty($re)) { foreach ($re as $element) { $title = "<a href='" . $element->getURL() . "' target='_blank'>" . $element->getTitle() . "</a>"; $url = "<a href='" . $element->getURL() . "' target='_blank'>" . substr($element->getURL(), 0, 80) . "</a>"; $description = $element->getSnippet(); // set variables $this->_objTpl->setVariable(array('DIRECTORY_FEED_DESCRIPTION' => strip_tags(substr($description, 0, 600)), 'DIRECTORY_FEED_TITLE' => $title, 'DIRECTORY_FEED_URL' => $url, 'DIRECTORY_FEED_DETAIL' => $_ARRAYLANG['TXT_DIRECTORY_DETAIL'], 'DIRECTORY_FEED_DETAIL_LINK' => $element->getURL(), 'DIRECTORY_FEED_VOTE' => $_ARRAYLANG['TXT_DIRECTORY_YOUR_VOTE'], 'DIRECTORY_FEED_VOTE_LINK' => $element->getURL(), 'DIRECTORY_FEED_AVERAGE_VOTE' => $url)); $this->_objTpl->parse('showResults'); } } }
</div> </body> <?php } else { ?> <body border="0" topmargin="0" leftmargin="0" marginwidth="0" marginheight="0" bgcolor="#000000" link="#000000" alink="#000000" vlink="#000000"> <div style="text-align: center; width:100%; height:100%; overflow: hidden"> <div style="width: 1260px; height: 100%"> <?php if (!isset($_GET['start'])) { $_GET['start'] = 0; } if (!isset($_GET['type'])) { $_GET['type'] = 'video'; } $gs = new GoogleSearch(); $searchtype = $_GET['type'] == 'video' ? GoogleSearch::VIDEO : GoogleSearch::IMAGE; $result = $gs->search($_GET['q'], $searchtype, 54, $_GET['start']); $i = 0; foreach ($result[$_GET['type']] as $image) { $i++; if (isset($_GET['type']) && $_GET['type'] == 'image') { echo '<a href="' . $image->originalContextUrl . '" target="_blank">'; echo '<img src="showthumb.php?image=' . rawurlencode($image->tbUrl) . '" border="0" title="[' . $image->titleNoFormatting . "] - " . $image->content . '"/>'; } else { echo '<a href="' . $image->playUrl . '" target="_blank">'; echo '<img src="showthumb.php?image=' . rawurlencode($image->tbUrl) . '" border="0" title="[' . $image->titleNoFormatting . "] - " . $image->content . " [Runtime: " . $image->duration . ' seconds]"/>'; } echo '</a>'; if ($i % 9 == 0) { echo '<br/>';
} //Instance of the http adapter, shared by aggregation through all classes $httpAdapter = new $parsedOptions["http-adapter"](); if (isset($parsedOptions["connect-timeout"])) { $httpAdapter->setConnectTimeout($parsedOptions["connect-timeout"]); } if (isset($parsedOptions["request-interval"]) && $parsedOptions["request-interval"] > 0) { $httpAdapter->setRequestInterval($parsedOptions["request-interval"]); } if (isset($parsedOptions["log-prepend-date"])) { $logger->useLogDate($parsedOptions["log-prepend-date"]); } //Check if youre bored and you just want to rule the world (?) ///////////////////////////////////////////////////////////////// if (in_array("google", array_keys($parsedOptions))) { $google = new GoogleSearch($httpAdapter, $logger); $google->setQuery($parsedOptions["google"]); isset($parsedOptions["google-language"]) ? $google->setLanguage($parsedOptions["google-language"]) : NULL; $offset = isset($parsedOptions["google-offset"]) ? $parsedOptions["google-offset"] : 0; $userTotal = isset($parsedOptions["google-max-results"]) ? $parsedOptions["google-max-results"] : 0; $sites = googleSearch($google, $offset, $userTotal); if (isset($parsedOptions["google-shuffle-sites"])) { shuffle($sites); } } if (isset($parsedOptions["omit-sites"])) { filterSites($sites, $logger, $parsedOptions["omit-sites"]); } $logger->setPrepend(""); //Check if url vars where passed,if not, we crawl the url /////////////////////////////////////////////////////////////////
<pre><?php if (!isset($_GET['start'])) { $_GET['start'] = 0; } $gs = new GoogleSearch(); $result = $gs->search($_GET['q'], GoogleSearch::IMAGE, 54, $_GET['start']); $i = 0; print_r($result); ?> <?php class GoogleSearch { /**************************** * GoogleSearch::search() * * @author Viper-7 (viper7@viper-7.com) * @date 2009-7-12 * * @param search String to search for * @param numresults Number of results to return per search type (max 4) * @param searchType Types to search for, can be a combination of several ie SEARCH_WEB | SEARCH_VIDEO | SEARCH_IMAGE * * @return Array of result objects * ****************************/ const WEB = 1; const LOCAL = 2; const VIDEO = 4; const IMAGE = 8; const BLOG = 16;