PHP
  Home arrow PHP arrow Page 4 - 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? 
Google.com  
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 - Working with Yahoo! Image Search Service
    ( Page 4 of 4 )

    As you may have guessed, defining a custom PHP function that returns the results produced by the Yahoo! Image Search Service to client code is actually a process nearly identical to the one I discussed when working with video searches. Therefore, I’m not going to bore you with more explanations of how this brand new PHP function will work.

    Instead, I’m going to show you its definition directly, in conjunction with a concrete example of how to use it.

    Having clarified this point, here’s the corresponding signature of this custom PHP function:


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


    // define 'getImageSearchResults()' function

    function getImageSearchResults($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/ImageSearchService
    /V1/imageSearch?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;

    }


    try{

    // call 'getImageSearchResults()' function

    echo getImageSearchResults();

    }

    catch(Exception $e){

    echo $e->getMessage();

    exit();

    }


    As I stated earlier, the above “getImageSearchResults()” is actually simple to grasp, since it implements practically the same business logic that you learned with the other Yahoo! web services. Anyway, to complete the explanation of this function, below I listed some real results returned by it when using the default “Madonna” search term:


    // displays the following


    Madonna_vogue.jpg


    Madonna_3.jpg 30-Apr-2005 14:34 23k Madonna_9.jpg 30-Apr-2005
    14:34 23k Madonna_vogue.jpg 30-Apr-2005 14:35 22k
    Madonna_13.jpg 30-Apr-2005 14:34 22k


    http://www.multinet.no/~jonarne/Hjemmesia/Favorittartister/
    madonna/Madonna_vogue.jpg

    madonna_90563a.jpg


    ANGRER: Madonna spiller gjerne på sex for å provosere,
    men angrer på boken Sex.- Jeg lurer stadig på hva som
    motiverte meg til å lage den, sier hun om sin vågale bok, Sex ,
    som vakte


    http://pub.tv2.no/multimedia/TV2/archive/00090/madonna_90563a.jpg

    madonna-079.jpg


    madonna 079


    http://www.celebritypicturesarchive.com/pictures/m/madonna/madonna-079.jpg

    Joey Bday Madonna party017.jpg


    Joey Bday Madonna party017


    http://www.tock.org/~casalila/new/mainpics/Casa%20Lila/
    Bday%20Parties/Joey's%20Bday%20party%20Madonna%
    20Party%204-98/Joey%20Bday%20Madonna%20party017.jpg

    madonna-with-fish-red.jpg


    madonna-with-fish-red.jpg


    http://www.nndb.com/people/480/000023411/madonna-with-fish-red.jpg

    30-madonna-britney.jpg


    30 madonna britney


    http://www.gmax.co.za/feel/08/pics/30-madonna-britney.jpg

    madonna_out_cover.jpg


    Could Madonna's publicist explain why he gave the authorization
    to publish such a bad photo for the cover of Out ! Anyway, Madonna
    will take the stage next month at the seventh annual


    http://www.ohlalaparis.com/photos/uncategorized/madonna_out_cover.jpg

    madonna_116044a.jpg


    BLIR SAKSØKT: Madonna kan ha latt seg inspirere for mye av Vogue-bilder.


    http://pub.tv2.no/multimedia/TV2/archive/00116/madonna_116044a.jpg

    madonna_reinvention6.jpg


    Madonna is back!


    http://schoolkrant.hsdrenthe.nl/archives/madonna_reinvention6.jpg

    madonna16.jpg


    Fotus da melhor cantora, mulher, mae, esposa, dançarina...
    Enfim, a melhor humana!!! Lindaaaaaaaaaaaaaaa!!!
    Don't tell me TE AMOOOOOOOO!!!


    http://livinha_minsane.zip.net/images/madonna16.jpg


    Evidently Madonna generates passionate comments all over the world, but as you know, you should stay focused (at least for now) on studying and eventually improving the source code of all the custom PHP functions that you saw here, concerning the use of the most popular web services offered by Yahoo!

    Final thoughts

    In this fourth part of the series, I showed you how to create a few simple custom PHP functions for working with some of the most useful web search services provided by Yahoo! In this way, we build small pieces of code that can be reused any number of times.

    In the next tutorial of the series, things will get even more exciting, since I’m going to teach you how to use the object-oriented paradigm to create a bunch of PHP 5 classes that will work in conjunction with all of the web services that were discussed earlier. You won't want to miss it!



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

       

    PHP ARTICLES

    - 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
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek