According to the concepts deployed in the introduction, my intention here is simply to demonstrate how to use an object-based approach to implement a specific Yahoo! Web Search Service. Based upon this objective, first I’m going to show you how to create a compact, highly modular PHP 5 class, which will be responsible first for querying the Yahoo! Web Search Service, and then returning the potential results neatly formatted to client code. That being said, pay close attention to the signature of the following class, which in this case is tasked with using the aforementioned Yahoo! web service: // example using Yahoo! Web Search Service - results are displayed in a basic (X)HTML format utilizing a class // define ' WebSearchService' class class WebSearchService{ private $requestPath='http://api.search.yahoo.com/WebSearchService/V1/webSearch? private $query='madonna'; private $numResults=10; private $output=''; public function __construct(){} // set search query public function setQuery($query){ if(!$query){ throw new Exception('Invalid search query!'); } $this->query=urlencode($query); } // set number of search results public function setNumResults($numResults){ if(!$numResults||!is_int($numResults)||$numResults>50){ throw new Exception('Invalid number of results!'); } $this->numResults=$numResults; } // perform web search and get search results public function getSearchResults(){ if(!$results=file_get_contents($this->requestPath.'&query='.$this- throw new Exception('Error requesting Yahoo! Web service'); } $results=unserialize($results); foreach($results[ResultSet][Result] as $result){ $this->output.='<h2>'.$result[Title].'</h2><p>'.$result[Summary].'</p><a } return $this->output; } } If you study the signature that corresponds to the above “WebSearchService” class that I just built in detail, then you’ll quickly grasp the way it works. Basically, the class in question exposes three primary methods aside from the pertinent constructor. The first two, called “setQuery()” and “setNumResults()” respectively, become very convenient for specifying (in that order) the search string that will be used to query the service and the number of results that should be returned to the browser. So far, nothing unexpected. Obviously, the workhorse of the class is its “getSearchResults()” method, which implements the necessary logic to retrieve the corresponding search results, which are finally formatted and returned to client code. As you can see, building a class that has the capacity for implementing the Yahoo! Web Search Service is not only simple, but also fun! Undoubtedly, the signature of the previous class can be largely improved, but it truly does a decent job using the mentioned web service. Okay, now that you learned how to build a PHP 5 class capable of implementing the Yahoo! Web Search Service with minor hassles, I believe that it's time to move on and see how it functions in the context of a real-word example. Thus, go ahead and read the next section to see how the previous “WebSearchService” class can be put to work with minor efforts.
blog comments powered by Disqus |
|
|
|
|
|
|
|