[1] | 1 | <?php |
---|
| 2 | /** |
---|
[135] | 3 | * Google_API.inc.php |
---|
| 4 | * code by strangecode :: www.strangecode.com :: this document contains copyrighted information |
---|
| 5 | * |
---|
[1] | 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: |
---|
[42] | 14 | // |
---|
[1] | 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 | |
---|
[135] | 30 | class Google_API { |
---|
[1] | 31 | |
---|
| 32 | /** |
---|
| 33 | * @var string |
---|
| 34 | * @access private |
---|
| 35 | */ |
---|
| 36 | var $_licenseKey = ''; |
---|
[42] | 37 | |
---|
[1] | 38 | /** |
---|
| 39 | * @var object |
---|
| 40 | * @access private |
---|
| 41 | */ |
---|
| 42 | var $_soapClient = NULL; |
---|
[42] | 43 | |
---|
[1] | 44 | /** |
---|
| 45 | * Constructor. |
---|
| 46 | * |
---|
| 47 | * @param string |
---|
| 48 | * @access public |
---|
| 49 | */ |
---|
| 50 | function GoogleAPI($licenseKey) |
---|
| 51 | { |
---|
| 52 | $this->_licenseKey = $licenseKey; |
---|
| 53 | $this->_soapClient = new SOAP_Client( |
---|
| 54 | 'http://api.google.com/search/beta2' |
---|
| 55 | ); |
---|
| 56 | } |
---|
[42] | 57 | |
---|
[1] | 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 | } |
---|
[42] | 79 | |
---|
[1] | 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 | } |
---|
[42] | 97 | |
---|
[1] | 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 | } |
---|
[42] | 126 | |
---|
[1] | 127 | /** |
---|
| 128 | * @param string |
---|
| 129 | * @param array |
---|
| 130 | * @return mixed |
---|
| 131 | * @access private |
---|
| 132 | */ |
---|
| 133 | function _performAPICall($apiCall, $parameters) |
---|
| 134 | { |
---|
| 135 | $result = $this->_soapClient->call( |
---|
| 136 | $apiCall, |
---|
| 137 | $parameters, |
---|
| 138 | 'urn:GoogleSearch' |
---|
| 139 | ); |
---|
| 140 | if (!PEAR::isError($result)) { |
---|
| 141 | return $result; |
---|
| 142 | } else { |
---|
| 143 | return false; |
---|
| 144 | } |
---|
| 145 | } |
---|
[42] | 146 | |
---|
[1] | 147 | /** |
---|
| 148 | * getFault |
---|
| 149 | * |
---|
| 150 | * returns a simple native php array containing the fault data |
---|
| 151 | * |
---|
[42] | 152 | * @return array |
---|
[1] | 153 | * @access public |
---|
| 154 | */ |
---|
| 155 | function getFault() |
---|
| 156 | { |
---|
| 157 | return $this->_soapClient->__getfault(); |
---|
| 158 | } |
---|
| 159 | } |
---|
| 160 | ?> |
---|
| 161 | |
---|
| 162 | |
---|