PHP
  Home arrow PHP arrow Page 4 - Building a Complete Web Searching Class with Yahoo Web Services and PHP 5
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PHP

Building a Complete Web Searching Class with Yahoo Web Services and PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 3
    2008-02-20


    Table of Contents:
  • Building a Complete Web Searching Class with Yahoo Web Services and PHP 5
  • Defining an image searching class in PHP 5
  • Using Inheritance to work with different Yahoo! Web Services
  • Consuming specific Yahoo! Web Search Services

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Building a Complete Web Searching Class with Yahoo Web Services and PHP 5 - Consuming specific Yahoo! Web Search Services
    ( Page 4 of 4 )

    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
     

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek