{"id":92,"date":"2006-03-05T22:38:00","date_gmt":"2006-03-05T22:38:00","guid":{"rendered":"http:\/\/blog.trungson.com\/?p=92"},"modified":"2006-03-05T22:38:00","modified_gmt":"2006-03-05T22:38:00","slug":"google-search-api-with-php5-or-nusoap","status":"publish","type":"post","link":"http:\/\/blog.trungson.com\/?p=92","title":{"rendered":"Google Search API with PHP5 or NuSOAP"},"content":{"rendered":"<p>If you want to search Google through their API. This class detects if you have the PHP5 SOAP lib to use or it should use the NuSOAP lib.<\/p>\n<p><textarea name=\"code\" class=\"php\"><br \/>\n<?php\n\/** \n* @author Son Nguyen\n* @since 3\/5\/2006\n* @package Framework\n* @subpackage WebServices\n*\/\nclass CGoogleAPI {\n private $mTryPHP5SOAP = TRUE; \/\/ try to use php5 soap lib\n private $mParams;\n \/** constructor *\/\n function __construct() {\n  \/\/ init param\n  $this->mParams = array(<br \/>\n   &#8216;key&#8217;=>&#8217;xxxxx&#8217;, \/\/ API key<br \/>\n   &#8216;q&#8217;=>NULL, \/\/ search term, will init later<br \/>\n   &#8216;start&#8217;=>0, \/\/ start from result n<br \/>\n   &#8216;maxResults&#8217;=>10, \/\/ number of results &#8211; max is 10<br \/>\n   &#8216;filter&#8217;=>TRUE, \/\/ filter &#8211; remove similar results<br \/>\n   &#8216;restrict&#8217;=>&#8221;, \/\/ restrict by topic<br \/>\n   &#8216;safeSearch&#8217;=>FALSE, \/\/ safesearch &#8211; remove adult links<br \/>\n   &#8216;lr&#8217;=>&#8221;, \/\/ restrict by language<br \/>\n   &#8216;ie&#8217;=>&#8221;, \/\/ ignore<br \/>\n   &#8216;oe&#8217;=>&#8221;, \/\/ ignore<br \/>\n  );<br \/>\n }<\/p>\n<p> \/** use nusoap *\/<br \/>\n function searchNUSOAP($pQuery) {<br \/>\n  include_once(&#8216;nusoap.php&#8217;);<\/p>\n<p>  \/\/ instantiate a new soap client:<br \/>\n  $vClient = new soapclient(&#8220;http:\/\/api.google.com\/search\/beta2&#8243;);<br \/>\n  \/\/ send the query off to the server, with our parameters and using the &#8216;doGoogleSearch&#8217; method<br \/>\n  $vResult = $vClient->call(&#8216;doGoogleSearch&#8217;,$this->mParams,&#8217;urn:GoogleSearch&#8217;);<br \/>\n  return $vResult;<br \/>\n }<\/p>\n<p> \/** try both php5 soap lib and if not available, use nusoap *\/<br \/>\n function search($pQuery) {<br \/>\n  $this->mParams[&#8216;q&#8217;] = $pQuery;<\/p>\n<p>  if (!class_exists(&#8216;SoapClient&#8217;) || !$this->mTryPHP5SOAP) {<br \/>\n   return $this->searchNUSOAP($pQuery);<br \/>\n  } \/\/ fi<\/p>\n<p>  \/\/ use php5 lib<br \/>\n  $vClient = new SoapClient(CHelperURL::getRelPath(__FILE__).&#8221;GoogleSearch.wsdl&#8221;,array(&#8220;trace&#8221;=>1,&#8221;exceptions&#8221;=>0));<br \/>\n  $vResult = $vClient->doGoogleSearch(<br \/>\n   $this->mParams[&#8216;key&#8217;],<br \/>\n   $this->mParams[&#8216;q&#8217;],<br \/>\n   $this->mParams[&#8216;start&#8217;],<br \/>\n   $this->mParams[&#8216;maxResults&#8217;],<br \/>\n   $this->mParams[&#8216;filter&#8217;],<br \/>\n   $this->mParams[&#8216;restrict&#8217;],<br \/>\n   $this->mParams[&#8216;safeSearch&#8217;],<br \/>\n   $this->mParams[&#8216;lr&#8217;],<br \/>\n   $this->mParams[&#8216;ie&#8217;],<br \/>\n   $this->mParams[&#8216;oe&#8217;]<br \/>\n  );<br \/>\n  return $vResult;<br \/>\n }<\/p>\n<p>}<br \/>\n?><br \/>\n<\/textarea><\/p>\n<p>With the search results in a PHP object, you should be able to manipulated the information easily.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you want to search Google through their API. This class detects if you have the PHP5 SOAP lib to use or it should use the NuSOAP lib. With the search results in a PHP object, you should be able to manipulated the information easily.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"http:\/\/blog.trungson.com\/index.php?rest_route=\/wp\/v2\/posts\/92"}],"collection":[{"href":"http:\/\/blog.trungson.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.trungson.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.trungson.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.trungson.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=92"}],"version-history":[{"count":0,"href":"http:\/\/blog.trungson.com\/index.php?rest_route=\/wp\/v2\/posts\/92\/revisions"}],"wp:attachment":[{"href":"http:\/\/blog.trungson.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=92"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.trungson.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=92"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.trungson.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=92"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}