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