PHP
  Home arrow PHP arrow Page 2 - Using Yahoo Web Services to Perform Searches with an Object-Oriented Approach
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

Using Yahoo Web Services to Perform Searches with an Object-Oriented Approach
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 2
    2008-02-12


    Table of Contents:
  • Using Yahoo Web Services to Perform Searches with an Object-Oriented Approach
  • Building a web search class with PHP 5
  • Testing the previous “WebSearchService” class
  • Building a video searching class with PHP 5

  • 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


    Using Yahoo Web Services to Perform Searches with an Object-Oriented Approach - Building a web search class with PHP 5
    ( Page 2 of 4 )

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

    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-
    >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;

    }

    }


    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.



     
     
    >>> 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 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek