[1] | 1 | <?php |
---|
| 2 | /** |
---|
[362] | 3 | * The Strangecode Codebase - a general application development framework for PHP |
---|
| 4 | * For details visit the project site: <http://trac.strangecode.com/codebase/> |
---|
[396] | 5 | * Copyright 2001-2012 Strangecode, LLC |
---|
[362] | 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 | /** |
---|
[136] | 24 | * Google_API.inc.php |
---|
| 25 | * |
---|
[1] | 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: |
---|
[42] | 33 | // |
---|
[1] | 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 | |
---|
| 47 | require_once 'SOAP/Client.php'; |
---|
| 48 | |
---|
[136] | 49 | class Google_API { |
---|
[1] | 50 | |
---|
| 51 | /** |
---|
| 52 | * @var string |
---|
| 53 | * @access private |
---|
| 54 | */ |
---|
[468] | 55 | private $_licenseKey = ''; |
---|
[42] | 56 | |
---|
[1] | 57 | /** |
---|
| 58 | * @var object |
---|
| 59 | * @access private |
---|
| 60 | */ |
---|
[468] | 61 | private $_soapClient = NULL; |
---|
[42] | 62 | |
---|
[1] | 63 | /** |
---|
| 64 | * Constructor. |
---|
| 65 | * |
---|
| 66 | * @param string |
---|
| 67 | * @access public |
---|
| 68 | */ |
---|
[468] | 69 | public function __construct($licenseKey) |
---|
[1] | 70 | { |
---|
| 71 | $this->_licenseKey = $licenseKey; |
---|
| 72 | $this->_soapClient = new SOAP_Client( |
---|
| 73 | 'http://api.google.com/search/beta2' |
---|
| 74 | ); |
---|
| 75 | } |
---|
[42] | 76 | |
---|
[1] | 77 | /** |
---|
| 78 | * Retrieves a page by URL from the Google Cache. |
---|
| 79 | * |
---|
| 80 | * @param string |
---|
| 81 | * @return mixed |
---|
| 82 | * @access public |
---|
| 83 | */ |
---|
[468] | 84 | public function getCachedPage($url) |
---|
[1] | 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 | } |
---|
[42] | 98 | |
---|
[1] | 99 | /** |
---|
| 100 | * Retrieves a spelling suggestion for a phrase. |
---|
| 101 | * |
---|
| 102 | * @param string |
---|
| 103 | * @return mixed |
---|
| 104 | * @access public |
---|
| 105 | */ |
---|
[468] | 106 | public function getSpellingSuggestion($phrase) |
---|
[1] | 107 | { |
---|
| 108 | return $this->_performAPICall( |
---|
| 109 | 'doSpellingSuggestion', |
---|
| 110 | array( |
---|
| 111 | 'key' => $this->_licenseKey, |
---|
| 112 | 'phrase' => $phrase |
---|
| 113 | ) |
---|
| 114 | ); |
---|
| 115 | } |
---|
[42] | 116 | |
---|
[1] | 117 | /** |
---|
| 118 | * Performs a web search. |
---|
| 119 | * |
---|
| 120 | * @param array |
---|
| 121 | * @return mixed |
---|
| 122 | * @access public |
---|
| 123 | */ |
---|
[468] | 124 | public function search($parameters = array()) |
---|
[1] | 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 | } |
---|
[42] | 145 | |
---|
[1] | 146 | /** |
---|
| 147 | * @param string |
---|
| 148 | * @param array |
---|
| 149 | * @return mixed |
---|
| 150 | * @access private |
---|
| 151 | */ |
---|
[468] | 152 | private function _performAPICall($apiCall, $parameters) |
---|
[1] | 153 | { |
---|
[195] | 154 | $app =& App::getInstance(); |
---|
| 155 | |
---|
[1] | 156 | $result = $this->_soapClient->call( |
---|
| 157 | $apiCall, |
---|
| 158 | $parameters, |
---|
| 159 | 'urn:GoogleSearch' |
---|
| 160 | ); |
---|
| 161 | if (!PEAR::isError($result)) { |
---|
| 162 | return $result; |
---|
| 163 | } else { |
---|
[195] | 164 | $app->logMsg(sprintf('Soap Pear error: %s', $result->getMessage()), LOG_NOTICE, __FILE__, __LINE__); |
---|
[1] | 165 | return false; |
---|
| 166 | } |
---|
| 167 | } |
---|
| 168 | } |
---|