PHP
  Home arrow PHP arrow Page 2 - Using Yahoo Web Services to Perform Se...
Dev Shed Forums 
Administration  
AJAX  
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 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
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

Using Yahoo Web Services to Perform Searches with an Object-Oriented Approach
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 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:
      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


    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


       · Over the course of this fifth chapter of the series, you’ll be provided with the...
     

       

    PHP ARTICLES

    - Building a Database-Driven Application with ...
    - User Authentication for a Project Management...
    - Introduction to the CodeIgniter PHP Framework
    - Adding Users for a Project Management Applic...
    - Migrating Class Code for a MIME Email to PHP...
    - Login and Logout Authentication for a Projec...
    - Composing Messages in HTML for MIME Email wi...
    - Project Management: Authentication
    - A Better Way to Determine MIME Types for MIM...
    - Project Management Overview
    - Handling Attachments in MIME Email with PHP
    - Completing the Project Management Application
    - Sending MIME Email with PHP
    - Handling Files for a Project Management Appl...
    - Viewing and Editing Tasks for a Project Mana...





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