* Copyright 2001-2012 Strangecode, LLC
*
* This file is part of The Strangecode Codebase.
*
* The Strangecode Codebase is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* The Strangecode Codebase is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* The Strangecode Codebase. If not, see .
*/
/**
* Google_API.inc.php
*
* Interface to the Google API using SOAP/Client.php.
*
* @author Quinn Comendant
* @version 1.1
*/
// Example of use:
//
// require_once 'Google_API.php';
// $google = new Google_API('your license key');
// $result = $google->search(
// array(
// 'query' => 'sebastian bergmann'
// )
// );
// if (false !== $result) {
// print_r($result);
// } else {
// echo 'Query failed.';
// }
require_once 'SOAP/Client.php';
class Google_API {
/**
* @var string
* @access private
*/
var $_licenseKey = '';
/**
* @var object
* @access private
*/
var $_soapClient = NULL;
/**
* Constructor.
*
* @param string
* @access public
*/
function Google_API($licenseKey)
{
$this->_licenseKey = $licenseKey;
$this->_soapClient = new SOAP_Client(
'http://api.google.com/search/beta2'
);
}
/**
* Retrieves a page by URL from the Google Cache.
*
* @param string
* @return mixed
* @access public
*/
function getCachedPage($url)
{
$result = $this->_performAPICall(
'doGetCachedPage',
array(
'key' => $this->_licenseKey,
'url' => $url
)
);
if ($result) {
$result = base64_decode($result);
}
return $result;
}
/**
* Retrieves a spelling suggestion for a phrase.
*
* @param string
* @return mixed
* @access public
*/
function getSpellingSuggestion($phrase)
{
return $this->_performAPICall(
'doSpellingSuggestion',
array(
'key' => $this->_licenseKey,
'phrase' => $phrase
)
);
}
/**
* Performs a web search.
*
* @param array
* @return mixed
* @access public
*/
function search($parameters = array())
{
if (!isset($parameters['query'])) {
return false;
}
return $this->_performAPICall(
'doGoogleSearch',
array(
'key' => $this->_licenseKey,
'q' => $parameters['query'],
'start' => isset($parameters['start']) ? $parameters['start'] : 0,
'maxResults' => isset($parameters['maxResults']) ? $parameters['maxResults'] : 10,
'filter' => isset($parameters['filter']) ? $parameters['filter'] : false,
'restrict' => isset($parameters['restrict']) ? $parameters['restrict'] : '',
'safeSearch' => isset($parameters['safeSearch']) ? $parameters['safeSearch'] : false,
'lr' => isset($parameters['lr']) ? $parameters['lr'] : '',
'ie' => isset($parameters['ie']) ? $parameters['ie'] : '',
'oe' => isset($parameters['oe']) ? $parameters['oe'] : ''
)
);
}
/**
* @param string
* @param array
* @return mixed
* @access private
*/
function _performAPICall($apiCall, $parameters)
{
$app =& App::getInstance();
$result = $this->_soapClient->call(
$apiCall,
$parameters,
'urn:GoogleSearch'
);
if (!PEAR::isError($result)) {
return $result;
} else {
$app->logMsg(sprintf('Soap Pear error: %s', $result->getMessage()), LOG_NOTICE, __FILE__, __LINE__);
return false;
}
}
}
?>