1<?php 2 3require_once(dirname(__FILE__).'/nusoap.php'); 4 5class GoogleAPI { 6 7 var $apikey = 'NOT SET'; 8 var $soapclient = null; 9 var $soapoptions = 'urn:GoogleSearch'; 10 var $restrict = ''; //set to restrict to certain page 11 var $error = ''; 12 13 /** 14 * Constructor 15 * 16 * @param string $key - Your Google API key 17 * @param string $site - Optional domainname for restricting the search 18 */ 19 function GoogleAPI($key,$site=''){ 20 $this->apikey = $key; 21 $this->soapclient = new soapclient('http://api.google.com/search/beta2'); 22 $this->soapclient->soap_defencoding='utf-8'; 23 $this->soapclient->decode_utf8=false; 24 if($site){ 25 $this->restrict = 'site:'.$site; 26 } 27 } 28 29 /** 30 * Calls the Google API and retrieves the search results in $ret 31 * 32 * Note that we pass in an array of parameters into the Google search. 33 * The parameters array has to be passed by reference. 34 * The parameters are well documented in the developer's kit on the 35 * Google site http://www.google.com/apis 36 */ 37 function do_search( $q, $start, $num=10 ){ 38 39 $params = array( 40 'key' => $this->apikey, 41 'q' => $this->restrict.' '.$q, 42 'start' => $start, 43 'maxResults' => $num, 44 'filter' => false, 45 'restrict' => '', 46 'safeSearch' => false, 47 'lr' => '', 48 'ie' => '', 49 'oe' => '' 50 ); 51 52 // Here's where we actually call Google using SOAP. 53 // doGoogleSearch is the name of the remote procedure call. 54 55 $ret = $this->soapclient->call('doGoogleSearch', $params, $this->soapoptions); 56 $err = $this->soapclient->getError(); 57 58/* soap debugging 59echo 'Request: <xmp>'.$this->soapclient->request.'</xmp>'; 60echo 'Response: <xmp>'.$this->soapclient->response.'</xmp>'; 61echo 'Debug log: <pre>'.$this->soapclient->debug_str.'</pre>'; 62*/ 63 64 if ($err) { 65 $this->error = $err; 66 return false; 67 } 68 69 return $ret; 70 } 71 72 /** 73 * Calls the Google API and retrieves the suggested spelling correction 74 */ 75 function do_spell( $q, &$spell ){ 76 77 $params = array( 78 'key' => $this->apikey, 79 'phrase' => $q, 80 ); 81 82 $spell = $soapclient->call('doSpellingSuggestion', $params, $this->soapoptions); 83 84 $err = $soapclient->getError(); 85 86 if ($err){ 87 $this->error = $err; 88 return false; 89 } 90 return $spell; 91 } 92} 93 94