source: trunk/lib/Google_API.inc.php @ 1

Last change on this file since 1 was 1, checked in by scdev, 19 years ago

Initial import.

File size: 3.9 KB
Line 
1<?php
2/**
3 * Interface to the Google API using SOAP/Client.php.
4 *
5 * Code by Strangecode :: www.strangecode.com :: This document contains copyrighted information.
6 * @author  Quinn Comendant <quinn@strangecode.com>
7 * @version 1.1
8 */
9
10// Example of use:
11//
12// require_once 'Google_API.php';
13// $google = new Google_API('your license key');
14// $result = $google->search(
15//   array(
16//     'query' => 'sebastian bergmann'
17//   )
18// );
19// if (false !== $result) {
20//   print_r($result);
21// } else {
22//   echo 'Query failed.';
23// }
24
25require_once 'SOAP/Client.php';
26
27
28/**
29* PHP Interface to the Google API
30*
31* @author  Sebastian Bergmann <sb@sebastian-bergmann.de>
32* @access  public
33*/
34class Google_API
35{
36
37    /**
38     * @var    string
39     * @access private
40     */
41    var $_licenseKey = '';
42   
43    /**
44     * @var    object
45     * @access private
46     */
47    var $_soapClient = NULL;
48   
49    /**
50     * Constructor.
51     *
52     * @param  string
53     * @access public
54     */
55    function GoogleAPI($licenseKey)
56    {
57        $this->_licenseKey = $licenseKey;
58        $this->_soapClient = new SOAP_Client(
59          'http://api.google.com/search/beta2'
60        );
61    }
62   
63    /**
64     * Retrieves a page by URL from the Google Cache.
65     *
66     * @param  string
67     * @return mixed
68     * @access public
69     */
70    function getCachedPage($url)
71    {
72        $result = $this->_performAPICall(
73            'doGetCachedPage',
74            array(
75                'key' => $this->_licenseKey,
76                'url' => $url
77            )
78        );
79        if ($result) {
80            $result = base64_decode($result);
81        }
82        return $result;
83    }
84   
85    /**
86     * Retrieves a spelling suggestion for a phrase.
87     *
88     * @param  string
89     * @return mixed
90     * @access public
91     */
92    function getSpellingSuggestion($phrase)
93    {
94        return $this->_performAPICall(
95            'doSpellingSuggestion',
96            array(
97                'key'    => $this->_licenseKey,
98                'phrase' => $phrase
99            )
100        );
101    }
102   
103    /**
104     * Performs a web search.
105     *
106     * @param  array
107     * @return mixed
108     * @access public
109     */
110    function search($parameters = array())
111    {
112        if (!isset($parameters['query'])) {
113            return false;
114        }
115        return $this->_performAPICall(
116            'doGoogleSearch',
117            array(
118                'key'         => $this->_licenseKey,
119                'q'           => $parameters['query'],
120                'start'       => isset($parameters['start'])      ? $parameters['start']      : 0,
121                'maxResults'  => isset($parameters['maxResults']) ? $parameters['maxResults'] : 10,
122                'filter'      => isset($parameters['filter'])     ? $parameters['filter']     : false,
123                'restrict'    => isset($parameters['restrict'])   ? $parameters['restrict']   : '',
124                'safeSearch'  => isset($parameters['safeSearch']) ? $parameters['safeSearch'] : false,
125                'lr'          => isset($parameters['lr'])         ? $parameters['lr']         : '',
126                'ie'          => isset($parameters['ie'])         ? $parameters['ie']         : '',
127                'oe'          => isset($parameters['oe'])         ? $parameters['oe']         : ''
128            )
129        );
130    }
131   
132    /**
133     * @param  string
134     * @param  array
135     * @return mixed
136     * @access private
137     */
138    function _performAPICall($apiCall, $parameters)
139    {
140        $result = $this->_soapClient->call(
141            $apiCall,
142            $parameters,
143            'urn:GoogleSearch'
144        );
145        if (!PEAR::isError($result)) {
146            return $result;
147        } else {
148            return false;
149        }
150    }
151   
152    /**
153     * getFault
154     *
155     * returns a simple native php array containing the fault data
156     *
157     * @return array
158     * @access public
159     */
160    function getFault()
161    {
162        return $this->_soapClient->__getfault();
163    }
164}
165?>
166
167
Note: See TracBrowser for help on using the repository browser.