PHP
  Home arrow PHP arrow Page 3 - Defining Some Custom PHP Functions with Yahoo Web Services
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? 
PHP

Defining Some Custom PHP Functions with Yahoo Web Services
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 3
    2008-02-05


    Table of Contents:
  • Defining Some Custom PHP Functions with Yahoo Web Services
  • A procedural approach to Yahoo! Search Service: defining a custom PHP function
  • Using Yahoo! Video Search Service
  • Working with Yahoo! Image Search Service

  • 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


    Defining Some Custom PHP Functions with Yahoo Web Services - Using Yahoo! Video Search Service
    ( Page 3 of 4 )

    As you might guess, creating a custom PHP function that uses the Yahoo! Video Search Service is very similar to working with the traditional searches that you saw in the previous section. But to dissipate any possible doubts that you might have regarding this topic, below I included the definition of a brand new PHP function, which implements the helpful Yahoo! Video Search Service.

    Given that, this function, called “getVideoSearchResults()” (yes, my creativity with names continues to blow me away) looks like this:


    // example using Yahoo! Video Search Service - results are displayed in a basic (X)HTML format utilizing an user-defined PHP function


    // define 'getVideoSearchResults()' function

    function getVideoSearchResults($query='madonna',$numResults=10){

    if(!$query){

    throw new Exception('Invalid search query!');

    }

    if(!$numResults||!is_int($numResults)){

    throw new Exception('Invalid number of results!');

    }

    $query=urlencode($query);

    $request='http://search.yahooapis.com/VideoSearchService/V1/videoSearch?
    appid=Your-AP-ID&query='.$query.'&results='.$numResults.'&output=php';

    // trigger the http request

    if(!$results=file_get_contents($request)){

    throw new Exception('Error requesting Yahoo! Web service');

    }

    $results=unserialize($results);

    $output='';

    foreach($results[ResultSet][Result] as $result){

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

    }


    Evidently, the definition of the above custom PHP function is very similar to the one I used to work with conventional web searches. Of course, in this specific case, the function will fetch the corresponding search results from a different URL, and format them utilizing different elements of the pertinent output array.

    In addition, below I coded a simple script that first calls the previous “getVideoSearchResults()” PHP function, and then displays on the browser the results produced by the respective video search process. Have a look at the following code sample, please:


    try{

    // call 'getVideoSearchResults()' function

    echo getVideoSearchResults();

    }

    catch(Exception $e){

    echo $e->getMessage();

    exit();

    }


    // displays the following search results


    madonna_2004_montage.wmv

     

    , interviews, news footage and tour clips from New York and Paris....
    here's hoping 2005 is just as busy! Right Click And Save Target As
    Madonna's 2004 Movie Montage (WMV) - 7:44 - 21.8Mb
    Noticias madonna - Noticias madonna - Noticias madonna -


    http://madonnalicious.com/downloads/madonna_2004_montage.wmv

    madonna_earlscourt_190804.wmv


    http://www.madonnalicious.com/downloads/madonna_gelredome
    _090904.wmv madonna 9sep 2004
    http://www.madonnalicious.com/downloads/madonna_earlscourt
    _190804.wmv
    http://www.madonnalicious.com/downloads/madonna_earlscourt
    _dtm_190804.wmv


    http://www.madonnalicious.com/downloads/madonna_earlscourt_190804.wmv

    madonna.al.et.mpg


    Summary: Watch Madonna on Entertainment Tonight :
    Madonna discusses American Life with ET .mpg Noticias madonna -
    Noticias madonna - Noticias madonna -


    http://steveswartz.com/madonna.al/madonna.al.et.mpg

    madonna_jonathanross_ruperteverett.wmv


    And Save Target As Friday Night With Jonathan Ross -
    Rupert Everett On Madonna (WMV) - 1:25 - 2.45Mb JR
    Noticias madonna - Noticias madonna - Noticias madonna -


    http://www.madonnalicious.com/downloads/madonna
    _jonathanross_ruperteverett.wmv

    madonna_gos_240804.wmv


    has just aired on local TV note the sound sync maybe
    slightly off in the video file Right Click And Save Target As
    Madonnas Visit To Great Ormond Street London Tonight
    Report WMV 128 985Mb Noticias madonna -
    Noticias madonna - Noticias madonna -


    http://madonnalicious.com/downloads/madonna_gos_240804.wmv

    madonna_earlscourt_dtm_190804.wmv


    http://www.madonnalicious.com/downloads/madonna
    _earlscourt_190804.wmv
    http://www.madonnalicious.com/downloads/madonna
    _earlscourt_dtm_190804.wmv
    http://www.madonnalicious.com/downloads/madonna
    _gelredome_music_090904.wmv


    http://www.madonnalicious.com/downloads/madonna_
    earlscourt_dtm_190804.wmv

    The Androids: Do It With Madonna


    Music Video by The Androids. Song: Do It With Madonna.
    Label: Universal Records Released in 2003.


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

    Britney Spears: Me Against The Music - Featuring Madonna


    Music Video by Britney Spears. Song: Me Against The Music -
    Featuring Madonna. Label: Zomba Recording Corp. Released in 2003.


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

    madonna defends adoption decision


    pop star madonna insists she's done nothing wrong in adopting a
    baby boy from malawi, saying she has acted within the law.


    http://cosmos.bcst.yahoo.com/up/yaustralia/news?ch=248153&cl=997154

    Madonna's adoption woes continue


    Madonna's baby adoption has taken yet another turn. The father
    of 13-month-old David claims he never intended his son to be
    adopted by the pop diva, only for her to raise him.


    http://cosmos.bcst.yahoo.com/up/yaustralia/news?ch=248153&cl=1022403


    As you can see, building a custom PHP 5 function that implements the Yahoo! Video Search Service is only a matter of triggering an HTTP request to the correct URL and then parsing the results using a simple “foreach” loop. Period.

    And speaking of working with some of the most popular web services offered by Yahoo!, in the last section of this tutorial I’m going to teach you how to build yet another brand new PHP function, which this time will be tasked with working with its helpful image search service.

    Naturally, in order to learn the details about how this function will be created, you’ll have to click on the link below and keep reading. We’re almost finished!



     
     
    >>> More PHP Articles          >>> More By Alejandro Gervasio
     

       

    PHP ARTICLES

    - Using Directory Iterators to Build Loader Ap...
    - Using the spl_autoload() Functions to Build ...
    - Working Out of the Object Context to Build L...
    - Using the _autoload() Magic Function to Buil...
    - The Destruct Magic Function in PHP 5
    - The Autoload Magic Function in PHP 5
    - Developing a Recursive Loading Class for Loa...
    - The Sleep and Wakeup Magic Functions in PHP 5
    - Using the Clone Magic Function in PHP 5
    - Including Files Recursively with Loader Appl...
    - The Call Magic Function in PHP 5
    - Designing a Captcha System with PHP and MySQL
    - Using Static Methods to Build Loader Apps in...
    - The Isset and Unset Magic Functions in PHP 5
    - Advanced PHP Form Input Validation to Check ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT