[1] | 1 | <?php |
---|
| 2 | // |
---|
| 3 | // +----------------------------------------------------------------------+ |
---|
| 4 | // | PHP Interface to the Google API | |
---|
| 5 | // +----------------------------------------------------------------------+ |
---|
| 6 | // | This source file is subject to version 2.02 of the PHP license, | |
---|
| 7 | // | that is bundled with this package in the file LICENSE, and is | |
---|
| 8 | // | available at through the world-wide-web at | |
---|
| 9 | // | http://www.php.net/license/2_02.txt. | |
---|
| 10 | // | If you did not receive a copy of the PHP license and are unable to | |
---|
| 11 | // | obtain it through the world-wide-web, please send a note to | |
---|
| 12 | // | license@php.net so we can mail you a copy immediately. | |
---|
| 13 | // +----------------------------------------------------------------------+ |
---|
| 14 | // | Author: Sebastian Bergmann <sb@sebastian-bergmann.de> | |
---|
| 15 | // +----------------------------------------------------------------------+ |
---|
| 16 | // |
---|
| 17 | // Example of use: |
---|
| 18 | // |
---|
| 19 | // require_once 'SOAP_Google.php'; |
---|
| 20 | // $google = new SOAP_Google('your license key'); |
---|
| 21 | // $result = $google->search( |
---|
| 22 | // array( |
---|
| 23 | // 'query' => 'sebastian bergmann' |
---|
| 24 | // ) |
---|
| 25 | // ); |
---|
| 26 | // if (false !== $result) { |
---|
| 27 | // print_r($result); |
---|
| 28 | // } else { |
---|
| 29 | // echo 'Query failed.'; |
---|
| 30 | // } |
---|
| 31 | // |
---|
| 32 | |
---|
| 33 | require_once 'SOAP/Client.php'; |
---|
| 34 | |
---|
| 35 | |
---|
| 36 | /** |
---|
| 37 | * PHP Interface to the Google API |
---|
| 38 | * |
---|
| 39 | * @author Sebastian Bergmann <sb@sebastian-bergmann.de> |
---|
| 40 | * @access public |
---|
| 41 | */ |
---|
| 42 | class SOAP_Google |
---|
| 43 | { |
---|
| 44 | |
---|
| 45 | /** |
---|
| 46 | * @var string |
---|
| 47 | * @access private |
---|
| 48 | */ |
---|
| 49 | var $_licenseKey = ''; |
---|
| 50 | |
---|
| 51 | /** |
---|
| 52 | * @var object |
---|
| 53 | * @access private |
---|
| 54 | */ |
---|
| 55 | var $_soapClient = NULL; |
---|
| 56 | |
---|
| 57 | /** |
---|
| 58 | * Constructor. |
---|
| 59 | * |
---|
| 60 | * @param string |
---|
| 61 | * @access public |
---|
| 62 | */ |
---|
| 63 | function SOAP_Google($licenseKey) |
---|
| 64 | { |
---|
| 65 | $this->_licenseKey = $licenseKey; |
---|
| 66 | $this->_soapClient = new SOAP_Client( |
---|
| 67 | 'http://api.google.com/search/beta2' |
---|
| 68 | ); |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | /** |
---|
| 72 | * Retrieves a page by URL from the Google Cache. |
---|
| 73 | * |
---|
| 74 | * @param string |
---|
| 75 | * @return mixed |
---|
| 76 | * @access public |
---|
| 77 | */ |
---|
| 78 | function getCachedPage($url) |
---|
| 79 | { |
---|
| 80 | $result = $this->_performAPICall( |
---|
| 81 | 'doGetCachedPage', |
---|
| 82 | array( |
---|
| 83 | 'key' => $this->_licenseKey, |
---|
| 84 | 'url' => $url |
---|
| 85 | ) |
---|
| 86 | ); |
---|
| 87 | if ($result) { |
---|
| 88 | $result = base64_decode($result); |
---|
| 89 | } |
---|
| 90 | return $result; |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | /** |
---|
| 94 | * Retrieves a spelling suggestion for a phrase. |
---|
| 95 | * |
---|
| 96 | * @param string |
---|
| 97 | * @return mixed |
---|
| 98 | * @access public |
---|
| 99 | */ |
---|
| 100 | function getSpellingSuggestion($phrase) |
---|
| 101 | { |
---|
| 102 | return $this->_performAPICall( |
---|
| 103 | 'doSpellingSuggestion', |
---|
| 104 | array( |
---|
| 105 | 'key' => $this->_licenseKey, |
---|
| 106 | 'phrase' => $phrase |
---|
| 107 | ) |
---|
| 108 | ); |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | /** |
---|
| 112 | * Performs a web search. |
---|
| 113 | * |
---|
| 114 | * @param array |
---|
| 115 | * @return mixed |
---|
| 116 | * @access public |
---|
| 117 | */ |
---|
| 118 | function search($parameters = array()) |
---|
| 119 | { |
---|
| 120 | if (!isset($parameters['query'])) { |
---|
| 121 | return false; |
---|
| 122 | } |
---|
| 123 | return $this->_performAPICall( |
---|
| 124 | 'doGoogleSearch', |
---|
| 125 | array( |
---|
| 126 | 'key' => $this->_licenseKey, |
---|
| 127 | 'q' => $parameters['query'], |
---|
| 128 | 'start' => isset($parameters['start']) ? $parameters['start'] : 0, |
---|
| 129 | 'maxResults' => isset($parameters['maxResults']) ? $parameters['maxResults'] : 10, |
---|
| 130 | 'filter' => isset($parameters['filter']) ? $parameters['filter'] : false, |
---|
| 131 | 'restrict' => isset($parameters['restrict']) ? $parameters['restrict'] : '', |
---|
| 132 | 'safeSearch' => isset($parameters['safeSearch']) ? $parameters['safeSearch'] : false, |
---|
| 133 | 'lr' => isset($parameters['lr']) ? $parameters['lr'] : '', |
---|
| 134 | 'ie' => isset($parameters['ie']) ? $parameters['ie'] : '', |
---|
| 135 | 'oe' => isset($parameters['oe']) ? $parameters['oe'] : '' |
---|
| 136 | ) |
---|
| 137 | ); |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | /** |
---|
| 141 | * @param string |
---|
| 142 | * @param array |
---|
| 143 | * @return mixed |
---|
| 144 | * @access private |
---|
| 145 | */ |
---|
| 146 | function _performAPICall($apiCall, $parameters) |
---|
| 147 | { |
---|
| 148 | $result = $this->_soapClient->call( |
---|
| 149 | $apiCall, |
---|
| 150 | $parameters, |
---|
| 151 | 'urn:GoogleSearch' |
---|
| 152 | ); |
---|
| 153 | if (!PEAR::isError($result)) { |
---|
| 154 | return $result; |
---|
| 155 | } else { |
---|
| 156 | return false; |
---|
| 157 | } |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | /** |
---|
| 161 | * getFault |
---|
| 162 | * |
---|
| 163 | * returns a simple native php array containing the fault data |
---|
| 164 | * |
---|
| 165 | * @return array |
---|
| 166 | * @access public |
---|
| 167 | */ |
---|
| 168 | function getFault() |
---|
| 169 | { |
---|
| 170 | return $this->_soapClient->__getfault(); |
---|
| 171 | } |
---|
| 172 | } |
---|
| 173 | ?> |
---|
| 174 | |
---|
| 175 | |
---|