PHP
  Home arrow PHP arrow Page 2 - 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 
IBM Rational Software Development Conference
 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 - Defining an image searching class in PHP 5
    (Page 2 of 4 )

    I need to show you something before I teach you how to use Inheritance to build some child classes that will concretely implement each of the Yahoo! Web Search Services discussed in the previous articles of the series. Below you will see the corresponding definition of a simple class that is tasked with consuming the Yahoo Image Search Service, which was left undefined in the last tutorial.

    Here's the signature of this brand new PHP class, which is responsible for performing image searches using the corresponding Yahoo API. Take a look at it, please:


    // example using Yahoo! Image Search Service - results are displayed in a basic (X)HTML format utilizing a class


    // define 'ImageSearchService' class

    class ImageSearchService{

    private $requestPath='http://search.yahooapis.com/
    ImageSearchService/V1/imageSearch?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><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;

    }

    }


    As you can see, the definition of the above "ImageSearchService" class looks very similar to the ones that I defined in the previous tutorial, which were aimed at working with the respective Yahoo traditional and image web searches. In this case, I built this class so it can perform image searches very easily, which is certainly a process that's clearly demonstrated by the following hands-on example:


    try{

    // create new instance of 'ImageSearchService' class

    $is=new ImageSearchService();

    // set search query

    $is->setQuery('Dido');

    // set number of search results

    $is->setNumResults(10);

    // display search results

    echo $is->getSearchResults();

    }

    catch(Exception $e){

    echo $e->getMessage();

    exit();

    }


    // displays the following

    dido_270.jpe


    http://fanwebdido.wz.cz/fotogalerie/dido_270.jpe

    Dido-Life_For_Rent-Frontal.jpg


    Enrique Iglesias - 7 50 cent-Get rich or die.. Dido - Life for rent
    Safri duo - 3.0 (new)


    http://www.coveralia.com/audio/d/Dido-Life_For_Rent-
    Frontal.jpg

    Dido_kontje.jpg


    http://xs45.xs.to/pics/05365/Dido_kontje.jpg

    dido_1.jpg


    http://peoples.ru/art/music/pop/dido/dido_1.jpg

    Dido.jpg


    http://biografie.leonardo.it/img/bio/d/Dido.jpg

    dido.jpg


    Pop chanteuse Dido entered London's Guildhall School of
    Music at age six , and had mastered the piano, the violin
    and the recorder by the time she was 10. After touring with a


    http://www.grabow.biz/images/dido.jpg

    dido.jpg


    dezembro 25, 2005 Parabans 1954 - Annie Lennox , cantora
    1971 - Dido , cantora Publicado por nelourenco Ă s


    http://atuleirus.weblog.com.pt/arquivo/dido.jpg

    dido128.jpg


    Dido: 'I camped out the night before Wham! The Final'.
    Photo: Pete Millson


    http://image.guardian.co.uk/sys-images/Guardian/Pix/arts/
    2003/10/02/dido128.jpg

    Dido2654.jpg


    http://www.sirensofsong.com/Dido/images/Dido2654.jpg

    INT-Dido.jpg


    Dido Born Florian Cloud de Bounevialle Armstrong, on the
    25th of December 1971, in England, Dido is the sister of
    Faithless mastermind and leading UK dance music producer,
    Rollo. She


    http://www.annecarlini.com/images/exclusive/interviews/INT-Dido.jpg


    Undoubtedly, consuming the Yahoo! Image Search Service by using the class that I built earlier is not only a no-brainer process that can be tackled with minor effort, but a fun experience! Okay, I have to admit that it actually isn't so fun for some, but keep in mind that I have to market this class to you somehow.

    Well, I assume at this time that the previous example should give you a clear idea of how to build a PHP class that utilizes the Yahoo! Image Search Service in a useful fashion. So, what's the next step?

    In accordance with the concepts that I deployed in the beginning, in the next section I'm going to teach you how to use Inheritance to create different subclasses. Each of them will be focused on consuming a specific Yahoo! Web service. In this manner, we will develop a more efficient object-oriented approach that will work with the services in question.

    To find out how these subclasses will be built, please jump ahead and read the next few lines.

    More PHP Articles
    More By Alejandro Gervasio


       · 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 1 hosted by Hostway