Home arrow PHP arrow Page 3 - Implementing Yahoo Image Search Web Service with PHP 5

Implementing the Image Search Service in PHP 5 - PHP

Welcome to the third installment of the series “Using Yahoo! Web Services with PHP 5.” If you’re a PHP programmer who wants to learn how to incorporate the useful web services offered by Yahoo, then this series of articles might be what you’re looking for. You’ll find numerous code samples to help you start using these popular web services within your own PHP 5 applications.

TABLE OF CONTENTS:
  1. Implementing Yahoo Image Search Web Service with PHP 5
  2. Using the Yahoo! Web Search Service with PHP 5
  3. Implementing the Image Search Service in PHP 5
  4. Implementing the Yahoo! Image Search Service with PHP 5
By: Alejandro Gervasio
Rating: starstarstarstarstar / 3
January 29, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

As I explained in the section that you just read, Yahoo! also provides PHP developers with a couple of powerful APIs that can be used within a given PHP 5 application to implement the popular video and image web search services with minor difficulties. As you’ll see in a few moments, these services can be easily queried by implementing practically the same programming logic that you learned in previous examples on this tutorial. This means that you shouldn’t have major problems putting the services to work for you.

But it’s time to move on from this boring theory since I assume you’re hungry to see some concrete code samples, right? Thus, I’m going to begin by demonstrating how to invoke the helpful Yahoo! Video Search Service from a primitive PHP 5 script. It also will be capable of displaying the corresponding results in (X)HTML format.

Now that I have explained how this brand new hands-on example is going to work, please pay attention to the following code sample to see the Yahoo! video search service in action:

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


try{

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

// trigger the http request

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

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

}

$results=unserialize($results);

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

echo '<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>';

}

}

catch(Exception $e){

echo $e->getMessage();

exit();

}

As you can see, the above PHP 5 script has a signature that’s very similar to the one that I defined to query the Yahoo! Web Search. However, there are some slight differences here, because in this particular case I’m using the Yahoo API to implement its helpful video search service. This means that the URL where the respective HTTP request is triggered is obviously distinct.

However, the rest of the script remains practically the same. The search results are first returned as a serialized PHP array and then displayed on the browser by using some basic (X)HTML tags. Besides, you should pay careful attention to the way that these results are extracted from the aforementioned array, since in this case I’m only interested in showing the pertinent titles, a brief summary on the retrieved video files, and the respective links.

Also, to complement the previous explanation, below I included an unformatted version of these search results, so you can grasp more clearly how the previous script functions:

// the following search results are displayed


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

Indeed, after analyzing the prior hands-on example, you’ll have to agree with me that calling the powerful Yahoo! Image Search Service from your own PHP application is a straightforward process that can be tackled pretty effortlessly. And if you still have your doubts as to my claims, test the previous example on your web server (don’t forget to get an application ID first) and you’ll be amazed at how neatly it works!

I think it’s time to move on, since I plan to show you yet another practical example focused primarily on demonstrating how to implement the Yahoo! Image Search Service in PHP 5. But as you might have guessed, this topic will be covered in detail in the upcoming section, so click on the link below and keep reading.



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

blog comments powered by Disqus
   

PHP ARTICLES

- 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...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 2 - Follow our Sitemap

Dev Shed Tutorial Topics: