PHP
  Home arrow PHP arrow Page 4 - 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 video searching class with PHP 5
    ( Page 4 of 4 )

    As I suggested in the section that you just read, building a PHP 5 class that has the capacity to work with the Yahoo! Video Search Service is indeed a no-brainer process, since I’m going to use basically the same web searching class that you learned previously.

    Of course, instead of building a brand new class from scratch, I could derive a new one from the parent, but this would mean getting ahead of myself, and I don’t want to do that. So, let me show you the signature of this video searching class, which has been listed below:


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


    // define 'VideoSearchService' class


    class VideoSearchService{

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

    }

    }


    Didn’t I tell you before that building a video searching class was going to be a simple process? The brand new “VideoSearchService” class demonstrates what I said. In this case, the class in question implements the Yahoo! Video Search Service. It looks nearly identical to the one I built when working with the conventional search service.

    Once you grasp the logic implemented by the above video searching class, you should take a look at the following practical example, which shows how to use it in order to display some video entries associated with the search term “Dido”:


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

    }


    // displays the following


    Dido: Don't Leave Home


    Music Video by Dido. Song: Don't Leave Home. Label: Arista
    Released in 2004.


    http://mv.music.yahoo.com/system/admin/tools/getvideo?
    vid=2165529

    Dido: White Flag


    Music Video by Dido. Song: White Flag. Label: Arista Released in
    2003.


    http://mv.music.yahoo.com/system/admin/tools/getvideo?vid=
    2158072

    Dido: Thankyou


    Music Video by Dido. Song: Thankyou. Label: Arista Records Released
    in 2001.


    http://mv.music.yahoo.com/system/admin/tools/getvideo?vid=2152196

    dido-zmarsu.avi


    Video: 3469 (2.4 MB format: AVI) Dido z Marsu (8.0 MB) Dido madmix
    (7.6 MB)


    http://www.dido.cz/dido/video/dido-zmarsu.avi

    dido_nea.mpg


    Production of Mikado wins National Recognition Current
    Production Schedule 2004 clip of Dido and Aeneas Audition
    information for Orpheus in the Underworld


    http://www2.potsdam.edu/severtki/dido_nea.mpg

    dido-madmix.avi


    3469 (2.4 MB format: AVI) Dido z Marsu (8.0 MB) Dido madmix
    (7.6 MB) Dido mánie (7.2 MB)


    http://www.dido.cz/dido/video/dido-madmix.avi

    dido_sand_in_my_shoes_dsl.wmv


    http://mfile.akamai.com/5890/wmv/bmgfr.download.akamai.com/
    5890/windows_media/video/v_amel_bent_ma_philosophie_high.wmv
    Dido

    http://mfile.akamai.com/15453/wmv/mtvgermany.download.akamai.com/
    15453/wmv/ondemand/stars/dido/dido_sand_in_my_shoes_dsl.wmv
    Gwen stefani


    http://mfile.akamai.com/15453/wmv/mtvgermany.download.akamai.com/
    15453/wmv/ondemand/stars/dido/dido_sand_in_my_shoes_dsl.wmv

    dido-papuce.avi


    Dido madmix (7.6 MB) Dido mánie (7.2 MB) Dido papuče (4.7 MB)
    Karaoke Dido (4.8 MB)


    http://www.dido.cz/dido/video/dido-papuce.avi

    karaoke-dido.avi


    Dido mánie (7.2 MB) Dido papuče (4.7 MB) Karaoke Dido
    (4.8 MB) Audio:


    http://www.dido.cz/dido/video/karaoke-dido.avi

    v_dido_life_for_rent.rm


    Title: v dido life for rent Noticias Dido_Life for Rent - Noticias
    Dido_Life for Rent - Noticias Dido_Life for Rent -


    http://mfile.akamai.com/6070/rm/bmgsp.download.akamai.com/
    6070/video/v_dido_life_for_rent.rm

    dido.wmv


    Dido - 900k - dur. 1.18 min


    http://www.romaestate.net/edizione2004/video/dido.wmv

    dido


    dido mvmv


    http://bantan124.castpost.com/dido%20mv.wmv

    dido_thank_you_2.mov


    Dido, Here With Me (2)(.mov, 512k) Dido, Thank You (1)(.mov, 392k)
    Dido, Thank You (2)(.mov, 520k) Dido, Thank You (3)(.mov, 488k)


    http://www.rachelleb.com/images/2004_06_06/
    dido_thank_you_2.mov

    dido_thank_you_1.mov


    Dido, Here With Me (1)(.mov, 612k) Dido, Here With Me (2)
    (.mov, 512k) Dido, Thank You (1)(.mov, 392k) Dido, Thank You
    (2)(.mov, 520k)


    http://www.rachelleb.com/images/2004_06_06/
    dido_thank_you_1.mov

    dido_here_with_me_2.mov


    Johnathan Rice (.mov, 324k) Dido, Here With Me (1)(.mov, 612k)
    Dido, Here With Me (2)(.mov, 512k) Dido, Thank You (1)(.mov, 392k)


    http://www.rachelleb.com/images/2004_06_06/
    dido_here_with_me_2.mov


    As demonstrated above, the “VideoSearchService” class comes in handy for first querying the pertinent Yahoo! Video Service and finally displaying the corresponding results in the browser. Of course, in this case there’s plenty of room to improve the signature of this class, but this will be left as homework for you.

    Final thoughts

    In this fifth installment of the series, I showed you how to use an object-oriented approach in order to implement both the traditional and video web search services provided by Yahoo!. As you saw earlier, this process only requires building some easy-to-grasp PHP 5 classes, so you shouldn’t have major problems including them into your existing web applications.

    In the next tutorial, I’m going to continue demonstrating how to use the object-based paradigm, but this time to implement the Yahoo! Image Search Service. So now that you know what the upcoming article will be about, you don’t have any excuses to miss it!



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