source: tags/2.1.5/lib/Google_API.inc.php

Last change on this file was 377, checked in by quinn, 14 years ago

Releasing trunk as stable version 2.1.5

File size: 4.6 KB
Line 
1<?php
2/**
3 * The Strangecode Codebase - a general application development framework for PHP
4 * For details visit the project site: <http://trac.strangecode.com/codebase/>
5 * Copyright 2001-2010 Strangecode, LLC
6 *
7 * This file is part of The Strangecode Codebase.
8 *
9 * The Strangecode Codebase is free software: you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as published by the
11 * Free Software Foundation, either version 3 of the License, or (at your option)
12 * any later version.
13 *
14 * The Strangecode Codebase is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * details.
18 *
19 * You should have received a copy of the GNU General Public License along with
20 * The Strangecode Codebase. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23/**
24 * Google_API.inc.php
25 *
26 * Interface to the Google API using SOAP/Client.php.
27 *
28 * @author  Quinn Comendant <quinn@strangecode.com>
29 * @version 1.1
30 */
31
32// Example of use:
33//
34// require_once 'Google_API.php';
35// $google = new Google_API('your license key');
36// $result = $google->search(
37//   array(
38//     'query' => 'sebastian bergmann'
39//   )
40// );
41// if (false !== $result) {
42//   print_r($result);
43// } else {
44//   echo 'Query failed.';
45// }
46
47require_once 'SOAP/Client.php';
48
49class Google_API {
50
51    /**
52     * @var    string
53     * @access private
54     */
55    var $_licenseKey = '';
56
57    /**
58     * @var    object
59     * @access private
60     */
61    var $_soapClient = NULL;
62
63    /**
64     * Constructor.
65     *
66     * @param  string
67     * @access public
68     */
69    function Google_API($licenseKey)
70    {
71        $this->_licenseKey = $licenseKey;
72        $this->_soapClient = new SOAP_Client(
73          'http://api.google.com/search/beta2'
74        );
75    }
76
77    /**
78     * Retrieves a page by URL from the Google Cache.
79     *
80     * @param  string
81     * @return mixed
82     * @access public
83     */
84    function getCachedPage($url)
85    {
86        $result = $this->_performAPICall(
87            'doGetCachedPage',
88            array(
89                'key' => $this->_licenseKey,
90                'url' => $url
91            )
92        );
93        if ($result) {
94            $result = base64_decode($result);
95        }
96        return $result;
97    }
98
99    /**
100     * Retrieves a spelling suggestion for a phrase.
101     *
102     * @param  string
103     * @return mixed
104     * @access public
105     */
106    function getSpellingSuggestion($phrase)
107    {
108        return $this->_performAPICall(
109            'doSpellingSuggestion',
110            array(
111                'key'    => $this->_licenseKey,
112                'phrase' => $phrase
113            )
114        );
115    }
116
117    /**
118     * Performs a web search.
119     *
120     * @param  array
121     * @return mixed
122     * @access public
123     */
124    function search($parameters = array())
125    {
126        if (!isset($parameters['query'])) {
127            return false;
128        }
129        return $this->_performAPICall(
130            'doGoogleSearch',
131            array(
132                'key'         => $this->_licenseKey,
133                'q'           => $parameters['query'],
134                'start'       => isset($parameters['start'])      ? $parameters['start']      : 0,
135                'maxResults'  => isset($parameters['maxResults']) ? $parameters['maxResults'] : 10,
136                'filter'      => isset($parameters['filter'])     ? $parameters['filter']     : false,
137                'restrict'    => isset($parameters['restrict'])   ? $parameters['restrict']   : '',
138                'safeSearch'  => isset($parameters['safeSearch']) ? $parameters['safeSearch'] : false,
139                'lr'          => isset($parameters['lr'])         ? $parameters['lr']         : '',
140                'ie'          => isset($parameters['ie'])         ? $parameters['ie']         : '',
141                'oe'          => isset($parameters['oe'])         ? $parameters['oe']         : ''
142            )
143        );
144    }
145
146    /**
147     * @param  string
148     * @param  array
149     * @return mixed
150     * @access private
151     */
152    function _performAPICall($apiCall, $parameters)
153    {
154        $app =& App::getInstance();
155       
156        $result = $this->_soapClient->call(
157            $apiCall,
158            $parameters,
159            'urn:GoogleSearch'
160        );
161        if (!PEAR::isError($result)) {
162            return $result;
163        } else {
164            $app->logMsg(sprintf('Soap Pear error: %s', $result->getMessage()), LOG_NOTICE, __FILE__, __LINE__);
165            return false;
166        }
167    }
168}
169?>
170
171
Note: See TracBrowser for help on using the repository browser.