Home arrow PHP arrow Page 4 - Building a Complete Web Searching Class with Yahoo Web Services and PHP 5

Consuming specific Yahoo! Web Search Services - PHP

Welcome to the final part of the series “Using Yahoo! Web Services with PHP 5.” As the title claims, this series walks you through the basic concepts surrounding the correct utilization of the most relevant web search services provided by Yahoo! and puts the corresponding theory into practice by utilizing both procedural and object-oriented approaches.

TABLE OF CONTENTS:
  1. Building a Complete Web Searching Class with Yahoo Web Services and PHP 5
  2. Defining an image searching class in PHP 5
  3. Using Inheritance to work with different Yahoo! Web Services
  4. Consuming specific Yahoo! Web Search Services
By: Alejandro Gervasio
Rating: starstarstarstarstar / 3
February 20, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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/
WebSearchService/V1/webSearch?appid=Your-AP-ID';

}

// implement concretely 'getSearchResults' method

public function getSearchResults(){

if(!$results=file_get_contents($this->requestPath.'&query=
'.$this->query.'&results='.$this->numResults.'&output=php')){

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 href="'.$result[Url].'">'.$result[Url].'</a>';

}

return $this->output;

}

}



// derive 'VideoSearchService' class from parent 'SearchService'


class VideoSearchService extends SearchService{

public function __construct(){

parent::__construct();

$this->requestPath='http://search.yahooapis.com/
VideoSearchService/V1/videoSearch?appid=Your-AP-ID';

}

// implement concretely 'getSearchResults' method

public function getSearchResults(){

if(!$results=file_get_contents($this->requestPath.'&query='
.$this->query.'&results='.$this->numResults.'&output=php')){

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><p><img src="'.$result[Thumbnail][Url].
'" width="'.$result[Thumbnail][Width].'" height="'.$result
[Thumbnail][Height].'" /></p><a href="'.$result[Url].'">'.
$result[Url].'</a>';

}

return $this->output;

}

}


// derive 'ImageSearchService' class from parent 'SearchService'


class ImageSearchService extends SearchService{

public function __construct(){

parent::__construct();

$this->requestPath='http://search.yahooapis.com/
ImageSearchService/V1/imageSearch?appid=Your-AP-ID';

}

// implement concretely 'getSearchResults' method

public function getSearchResults(){

if(!$results=file_get_contents($this->requestPath.'&query='.
$this->query.'&results='.$this->numResults.'&output=php')){

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><p><img src="'.$result[Thumbnail][Url].'" width="'.$result
[Thumbnail][Width].'" height="'.$result[Thumbnail][Height].'" />
</p><a href="'.$result[Url].'">'.$result[Url].'</a>';

}

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!



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: