PHP
  Home arrow PHP arrow Page 4 - Building a Complete Web Searching Clas...
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
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? 
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: 5 stars5 stars5 stars5 stars5 stars / 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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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

    Dell PowerEdge Servers

    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!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · Over the course of this last episode of the series, you’ll learn how to build some...
     

       

    PHP ARTICLES

    - Setting Up a Web-based Image Hosting Service
    - Comparing Files and Databases with PHP Bench...
    - Setting Up a Web-Based Image Gallery
    - Using Timers to Benchmark PHP Applications
    - Benchmarking Applications with PHP
    - Setting Up a Web-Based File Manager: PHPfile...
    - Developing a Modular Class For a PHP File Up...
    - Setting Up a Web-Based File Manager: bfExplo...
    - Defining a Custom Function for File Uploader...
    - Parsing Child Nodes with the DOM XML extensi...
    - Creating an Error Handling Module for a PHP ...
    - Accessing Attributes and Cloning Nodes with ...
    - Retrieving Information on Selected Files wit...
    - Handling HTML Strings and Files with the DOM...
    - Building File Uploaders with PHP 5

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway