As I discussed in the last section, in this particular case I'm going to use the benefits of Inheritance to derive three subclasses from the "WebService" parent that you saw earlier. As you might have guessed at this point, each of these will concretely implement the generic "getSearchResults()" declared in the base class, something that certainly will come in handy for consuming a specific Yahoo! Web Search Service. Still with me? All right, now take a deep breath, relax, and have a look at the signatures of the following subclasses. Each of them works with a different web search service: // derive 'WebSearchService' class from parent 'SearchService' class WebSearchService extends SearchService{ public function __construct(){ parent::__construct(); $this->requestPath='http://api.search.yahoo.com/ } // implement concretely 'getSearchResults' method public function getSearchResults(){ if(!$results=file_get_contents($this->requestPath.'&query= 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 } return $this->output; } } // derive 'VideoSearchService' class from parent 'SearchService' class VideoSearchService extends SearchService{ public function __construct(){ parent::__construct(); $this->requestPath='http://search.yahooapis.com/ } // implement concretely 'getSearchResults' method public function getSearchResults(){ if(!$results=file_get_contents($this->requestPath.'&query=' 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 } return $this->output; } } // derive 'ImageSearchService' class from parent 'SearchService' class ImageSearchService extends SearchService{ public function __construct(){ parent::__construct(); $this->requestPath='http://search.yahooapis.com/ } // implement concretely 'getSearchResults' method public function getSearchResults(){ if(!$results=file_get_contents($this->requestPath.'&query='. 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]. } return $this->output; } } You'll have to agree that building some web searching classes that consume specific Yahoo! Web Services from a base class is a very comprehensive process that can be performed with minor difficulties. As you can see, in this case I derived three different child classes, called "WebSearchService," "VideoSearchService," and "ImageSearchService" respectively. And not surprisingly, each of them is focused on consuming a specific Yahoo! Web Service. Besides, it's worthwhile to note here that these subclasses concretely implement the same generic "getSearchResults()" method declared in the parent, which implies that any instance of them will certainly be a polymorphic object too. Well, now that you've grasped the logic that stands behind these web search subclasses, you may want to see how they work. So considering this possibility, below I included some practical examples that show these classes in action. Here they are: // example on using "Yahoo Web Search Service try{ // create new instance of 'WebSearchService' class $ws=new WebSearchService(); // set search query $ws->setQuery('Devshed.com'); // set number of search results $ws->setNumResults(15); // display search results echo $ws->getSearchResults();
} catch(Exception $e){ echo $e->getMessage(); exit(); } // example on using "Yahoo Video Search Service try{ // create new instance of 'VideoSearchService' class $vs=new VideoSearchService(); // set search query $vs->setQuery('dido'); // set number of search results $vs->setNumResults(15); // display search results echo $vs->getSearchResults();
} catch(Exception $e){ echo $e->getMessage(); exit(); } // example on using "Yahoo Image Search Service try{ // create new instance of 'ImageSearchService' class $is=new ImageSearchService(); // set search query $is->setQuery('dido'); // set number of search results $is->setNumResults(15); // display search results echo $is->getSearchResults();
} catch(Exception $e){ echo $e->getMessage(); exit(); } True to form, the above three examples show in a nutshell how each of the subclasses that I built before can be used to easily consume a specific Yahoo! Web Search Service. Also, for obvious reasons, I've not included the pertinent results produced by each of these examples, but at this stage you should have a very clear idea of how to incorporate the previous web searching classes (or a modified version of them) into your own PHP applications. Final thoughts It's hard to believe, but this is the end of the series. Nevertheless, I hope the overall experience has been pretty instructive, since in these tutorials you learned how to implement the most relevant web services provided by Yahoo! by using both procedural and object-oriented approaches in PHP 5. As you know, the web is becoming more and more dynamic each day, so if you want to add an extra "touch" to your PHP 5 applications, you may want to consider consuming the cool set of Yahoo! Web Services. See you in the next PHP development tutorial!
blog comments powered by Disqus |
|
|
|
|
|
|
|