Home arrow PHP arrow Page 4 - Defining Some Custom PHP Functions with Yahoo Web Services

Working with Yahoo! Image Search Service - PHP

Welcome to the fourth installment of the series “Using Yahoo! Web Services with PHP 5.” Made up of six approachable tutorials, this series provides you with the right pointers to start using these useful web services within the context of your PHP 5-driven applications. It complements the corresponding theoretical concepts with a decent variety of hands-on examples.

TABLE OF CONTENTS:
  1. Defining Some Custom PHP Functions with Yahoo Web Services
  2. A procedural approach to Yahoo! Search Service: defining a custom PHP function
  3. Using Yahoo! Video Search Service
  4. Working with Yahoo! Image Search Service
By: Alejandro Gervasio
Rating: starstarstarstarstar / 3
February 05, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: