PHP
  Home arrow PHP arrow Page 4 - Defining Some Custom PHP Functions wit...
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

Defining Some Custom PHP Functions with Yahoo Web Services
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 2
    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:
      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

    TestComplete™ automates software testing for a fraction of what the big guys charge. Easy functional and load testing for all Windows, .NET, Java and Web apps. Download a free trial now.

    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!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · In the last article, I discussed how to consume Yahoo! Image Search Service, by mean...
     

       

    PHP ARTICLES

    - 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
    - Working with Multiple Document Nodes with th...




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