Location: PHPKode > scripts > Google Developer API > google-developer-api/Google_API.php
<?
require_once 'SOAP/Client.php';

class Google_API {
    var $_soapURL = 'http://api.google.com/search/beta2';
    var $_soapOptions = array(  'namespace' => 'urn:GoogleSearch',
                                'trace' => 0
                            );

    var $_googleApiKey = '';

    function Google_API($apiKey = null) {
        if ( $apiKey ) {
            $this->setKey($apiKey);
        }
        $this->init();
    }

    function init() {
        $this->_soap = new SOAP_Client($this->_soapURL);
    }

    function setKey($apiKey = null) {
        if ( $apiKey ) {
            $this->_googleApiKey = $apiKey;
        }
    }

    /**
     *  Make the call to the Google SOAP interface.
     *
     *  @access     private
     *  @param      string      command     The Google command to execute
     *  @param      array       params      The paramaters to pass with the command
     *  @return     mixed                   Dependent on the commands WSDL configuration
     */
    function _call($command, $params) {
        if ( !$this->_googleApiKey ) {
            echo 'ERROR: You need to assign your Google API Key';
            return false;
        }

        $finalParams = array('key' => $this->_googleApiKey) + $params;
        if ( PEAR::isError($response = $this->_soap->call($command, $finalParams, $this->_soapOptions)) ) {
            echo 'ERROR [# '.$response->getCode().'] occurred!<br>
                    Message: '.$response->getMessage();
            return false;
        }
        return $response;
    }

    /**
     *  Retrieved the cached version of a URL from Google's index
     *  
     *  @access     public
     *  @param      string      url         The URL to fetch from the cached index
     *  @return     string                  The HTML from Google's index
     */
    function GetCachedPage($url) {
        $params = array(
                    'key' =>    $this->_googleApiKey,
                    'url' =>    $url,
                    );
        if ( $response = $this->_call('doGetCachedPage', $params) ) {
            return base64_decode($response);
        } else {
            return false;
        }
    }

    /**
     *  Request a spelling suggestion from Google for a specific phrase
     *  
     *  @access     public
     *  @param      string      phrase      The phrase to check the spelling of
     *  @return     string                  Google's suggestion (blank response can indicate successful match or no match)
     */
    function SpellingSuggestion($phrase) {
        $params = array(
                    'key' =>    $this->_googleApiKey,
                    'phrase' => $phrase,
                    );
        if ( $response = $this->_call('doSpellingSuggestion', $params) ) {
            return $response;
        } else {
            return false;
        }
    }

    function Search($query, $params) {
        if ( $params['maxResults'] > 10 ) {
            $params['maxResults'] = 10;
        }
        $finalParams = array('q' => $query) + $params;
        if ( $response = $this->_call('doGoogleSearch', $finalParams) ) {
            return $response;
        } else {
            return false;
        }
    }
}
?>
Return current item: Google Developer API