5 * @author: Chris Petersen, Reini Urban
8 Copyright (c) 2002 Intercept Vector
9 Copyright (c) 2004 Reini Urban
11 This library is free software; you can redistribute it and/or
12 modify it under the terms of the GNU Lesser General Public
13 License as published by the Free Software Foundation; either
14 version 2.1 of the License, or (at your option) any later version.
16 This library is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 Lesser General Public License for more details.
21 You should have received a copy of the GNU Lesser General Public
22 License along with this library; if not, write to the Free Software Foundation, Inc.,
23 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 If you have any questions or comments, please email:
28 admin@interceptvector.com
30 http://www.interceptvector.com
34 * @seealso: http://scripts.incutio.com/google/
38 * Objectified, simplified, documented and added the two other queries
43 * GoogleSearchResults, list of GoogleSearch Result Elements
45 * Each time you issue a search request to the Google service, a
46 * response is returned back to you. This section describes the
47 * meanings of the values returned to you.
49 * <documentFiltering> - A Boolean value indicating whether filtering
50 * was performed on the search results. This will be "true" only if
51 * (a) you requested filtering and (b) filtering actually occurred.
53 * <searchComments> - A text string intended for display to an end
54 * user. One of the most common messages found here is a note that
55 * "stop words" were removed from the search automatically. (This
56 * happens for very common words such as "and" and "as.")
58 * <estimatedTotalResultsCount> - The estimated total number of
59 * results that exist for the query. Note: The estimated number may
60 * be either higher or lower than the actual number of results that
63 * <estimateIsExact> - A Boolean value indicating that the estimate is
64 * actually the exact value.
66 * <resultElements> - An array of <resultElement> items. This
67 * corresponds to the actual list of search results.
69 * <searchQuery> - This is the value of <q> for the search request.
71 * <startIndex> - Indicates the index (1-based) of the first search
72 * result in <resultElements>.
74 * <endIndex> - Indicates the index (1-based) of the last search
75 * result in <resultElements>.
77 * <searchTips> - A text string intended for display to the end
78 * user. It provides instructive suggestions on how to use Google.
80 * <directoryCategories> - An array of <directoryCategory> items. This
81 * corresponds to the ODP directory matches for this search.
83 * <searchTime> - Text, floating-point number indicating the total
84 * server time to return the search results, measured in seconds.
87 class GoogleSearchResults
89 var $_fields = "documentFiltering,searchComments,estimatedTotalResultsCount,estimateIsExact,searchQuery,startIndex,endIndex,searchTips,directoryCategories,searchTime,resultElements";
90 var $resultElements, $results;
92 function GoogleSearchResults($result)
94 $this->fields = explode(',', $this->_fields);
95 foreach ($this->fields as $f) {
96 $this->{$f} = $result[$f];
99 $this->results = array();
100 //$this->resultElements = $result['resultElements'];
101 foreach ($this->resultElements as $r) {
102 $this->results[] = new GoogleSearchResult($r);
109 * Google Search Result Element:
111 * <summary> - If the search result has a listing in the ODP
112 * directory, the ODP summary appears here as a text string.
114 * <URL> - The URL of the search result, returned as text, with an
117 * <snippet> - A snippet which shows the query in context on the URL
118 * where it appears. This is formatted HTML and usually includes <B>
119 * tags within it. Note that the query term does not always appear
120 * in the snippet. Note: Query terms will be in highlighted in bold
121 * in the results, and line breaks will be included for proper text
124 * <title> - The title of the search result, returned as HTML.
126 * <cachedSize> - Text (Integer + "k"). Indicates that a cached
127 * version of the <URL> is available; size is indicated in
130 * <relatedInformationPresent> - Boolean indicating that the
131 * "related:" query term is supported for this URL.
133 * <hostName> - When filtering occurs, a maximum of two results from
134 * any given host is returned. When this occurs, the second
135 * resultElement that comes from that host contains the host name in
138 * <directoryCategory> - array with "fullViewableName" and
139 * "specialEncoding" keys.
141 * <directoryTitle> - If the URL for this resultElement is contained
142 * in the ODP directory, the title that appears in the directory
143 * appears here as a text string. Note that the directoryTitle may
144 * be different from the URL's <title>.
146 class GoogleSearchResult
148 var $_fields = "summary,URL,snippet,title,cachedSize,relatedInformationPresent,hostName,directoryCategory,directoryTitle";
150 function GoogleSearchResult($result)
152 $this->fields = explode(',', $this->_fields);
153 foreach ($this->fields as $f) {
154 $this->{$f} = $result[$f];
163 function Google($maxResults = 10, $license_key = false, $proxy = false)
166 $this->license_key = $license_key;
167 elseif (!defined('GOOGLE_LICENSE_KEY')) {
168 trigger_error("\nYou must first obtain a license key at http://www.google.com/apis/"
169 . "\nto be able to use the Google API." .
170 "\nIt's free however.", E_USER_WARNING);
173 $this->license_key = GOOGLE_LICENSE_KEY;
174 require_once 'lib/nusoap/nusoap.php';
176 $this->soapclient = new soapclient(SERVER_URL . NormalizeWebFileName("GoogleSearch.wsdl"), "wsdl");
177 $this->proxy = $this->soapclient->getProxy();
178 if ($maxResults > 10) $maxResults = 10;
179 if ($maxResults < 1) $maxResults = 1;
180 $this->maxResults = $maxResults;
187 * See http://www.google.com/help/features.html for examples of
188 * advanced features. Anything that works at the Google web site
189 * will work as a query string in this method.
191 * You can use the start and maxResults parameters to page through
192 * multiple pages of results. Note that 'maxResults' is currently
193 * limited by Google to 10. See the API reference for more
194 * advanced examples and a full list of country codes and topics
195 * for use in the restrict parameter, along with legal values for
196 * the language, inputencoding, and outputencoding parameters.
198 * <license key> Provided by Google, this is required for you to access the
199 * Google service. Google uses the key for authentication and
201 * <q> (See the API docs for details on query syntax.)
202 * <start> Zero-based index of the first desired result.
203 * <maxResults> Number of results desired per query. The maximum
204 * value per query is 10. Note: If you do a query that doesn't
205 * have many matches, the actual number of results you get may be
206 * smaller than what you request.
207 * <filter> Activates or deactivates automatic results filtering,
208 * which hides very similar results and results that all come from
209 * the same Web host. Filtering tends to improve the end user
210 * experience on Google, but for your application you may prefer
211 * to turn it off. (See the API docs for more
213 * <restrict> Restricts the search to a subset of the Google Web
214 * index, such as a country like "Ukraine" or a topic like
215 * "Linux." (See the API docs for more details.)
216 * <safeSearch> A Boolean value which enables filtering of adult
217 * content in the search results. See SafeSearch for more details.
218 * <lr> Language Restrict - Restricts the search to documents
219 * within one or more languages.
220 * <ie> Input Encoding - this parameter has been deprecated and is
221 * ignored. All requests to the APIs should be made with UTF-8
222 * encoding. (See the API docs for details.)
223 * <oe> Output Encoding - this parameter has been deprecated and is
224 * ignored. All requests to the APIs should be made with UTF-8
227 function doGoogleSearch($query, $startIndex = 1, $maxResults = 10, $filter = "false",
228 $restrict = '', $safeSearch = 'false', $lr = '',
229 $inputencoding = 'UTF-8', $outputencoding = 'UTF-8')
231 if (!$this->license_key)
233 // doGoogleSearch() gets created automatically!! (some eval'ed code from the soap request)
234 $result = $this->proxy->doGoogleSearch($this->license_key, // "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
242 $inputencoding, // ignored by server, everything is UTF-8 now
244 return new GoogleSearchResults($result);
248 * Retrieve a page from the Google cache.
250 * Cache requests submit a URL to the Google Web APIs service and
251 * receive in return the contents of the URL when Google's
252 * crawlers last visited the page (if available).
254 * Please note that Google is not affiliated with the authors of
255 * cached pages nor responsible for their content.
257 * The return type for cached pages is base64 encoded text.
259 * @params string url - full URL to the page to retrieve
260 * @return string full text of the cached page
262 function doGetCachedPage($url)
264 if (!$this->license_key)
266 // This method gets created automatically!! (some eval'ed code from the soap request)
267 $result = $this->proxy->doGetCachedPage($this->license_key,
269 if (!empty($result)) return base64_decode($result);
273 * Get spelling suggestions from Google
275 * @param string phrase word or phrase to spell-check
276 * @return string text of any suggested replacement, or None
278 function doSpellingSuggestion($phrase)
280 if (!$this->license_key)
282 // This method gets created automatically!! (some eval'ed code from the soap request)
283 return $this->proxy->doSpellingSuggestion($this->license_key,
292 // c-hanging-comment-ender-p: nil
293 // indent-tabs-mode: nil