Implementing Yahoo Image Search Web Service with PHP 5

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.

Implementing popular web services, like video and image searches, within your own web site can be a powerful mechanism and handy when it comes to keeping visitors engaged. The good news is that you, as a PHP developer, can easily take advantage of these services by using the set of thorough APIs provided by Yahoo!. So if you’re curious about how to do this, start reading this article now!

Now that I’ve introduced you to the main subject of this series, it’s time to rehash the interesting topics that I discussed in the preceding tutorial. In dong so, you’ll be able to easily recall all of the key concepts surrounding the implementation of the useful Yahoo! Web Search Service in conjunction with PHP 5.

Simply put, in the previous article of the series I provided you with a quick overview of how to integrate the aforementioned Yahoo Web Search Service by coding a basic PHP 5 script that was tasked with performing two primary tasks. First, it must query the service via the submission of a typical query string (as you’ll probably remember, the term “Madonna” was used in that case), and second, it must fetch the corresponding results in the form of a serialized PHP array.

Once the search results in question were retrieved by the script, they were properly formatted by using a few basic (X)HTML tags and displayed directly on the browser. Of course, the most important thing to stress here aside from the formatting process is that those results were returned as a multidimensional PHP array by including into the respective query string the “output=php” option, which enormously facilitates parsing any output produced by the web service. This is because this procedure involves only the utilization of some simple array processing functions bundled with PHP.

At this point, I’m reasonably sure that you’re familiar with parsing the eventual results returned by the Yahoo! Web Search Service when they’re retrieved as a serialized PHP array. So based on this assumption, in this third chapter of the series, I’m going to continue exploring in detail other helpful web services offered by Yahoo!, such as those focusing on searching images and videos. Also, I will demonstrate how they can be queried directly from a basic PHP 5 application and process the corresponding search results by utilizing the same group of array handling functions that you saw in the previous article of the series.

Hopefully, by the end of this tutorial you’ll be equipped with a decent background to start using these additional Yahoo! web services within your existing or future PHP 5-driven applications. Thus, don’t waste more time in preliminaries; begin reading now!

{mospagebreak title=Using the Yahoo! Web Search Service with PHP 5}

As you may have noticed, in many of my articles on PHP web development I like to reintroduce previous hands-on examples of how to perform a certain task with this great server-side scripting language. And this one won’t be an exception to the rule. Before I provide you with concrete code samples regarding the implementation of Yahoo!’s video and image web search services with PHP 5, I’m going to list the complete source code of the example that you learned in the previous tutorial. As you’ll probably recall, it showed you how to invoke the traditional Yahoo! Search Service.

So having said this, please take some time to look at the following code sample, which queries the web service in question with the search term “Madonna” and at the end, displays the returned results by using a few basic (X)HTML tags:

try{

$request=’http://api.search.yahoo.com/WebSearchService/V1/webSearch?
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><a href="’.$result
[Url].’">’.$result[Url].'</a>';

}

}

catch(Exception $e){

echo $e->getMessage();

exit();

}



// displays the following:


Madonna


Official site of pop diva Madonna, with news, music, media, and fan club.

http://www.madonna.com/


MySpace.com – Madonna – Pop / Rock – www.myspace.com/madonna


Madonna MySpace page with news, blog, music downloads, desktops, wallpapers, and more.

http://www.myspace.com/madonna

If you study closely the above practical example, you’ll see that implementing the Yahoo! Web Search Service with PHP 5 is a relatively simple process that can be tackled even if you’re a beginner at PHP programming. As shown by the previous code sample, first the script queries the mentioned service by using the corresponding application ID provided by Yahoo! along with the respective search term and the “output=php” option. Then the potential results are returned in the form of a serialized array. Pretty understandable, right?

Next, those results are properly unserialized and then displayed on the browser via a common “foreach” PHP statement. In addition, it’s worthwhile to highlight that the web service always sends the output wrapped in a “ResultSet” array element, which can be easily parsed by utilizing some regular PHP array handling functions.

So far, so good. At this stage you’ve hopefully recalled the basics for implementing the Yahoo! Web Search Service with PHP 5. Thus it’s an excellent moment to see how to apply the same business logic that you learned previously to use the respective image and video search services.

Therefore, considering that learning how to incorporate these two useful Yahoo! web services into your PHP applications can potentially be interesting, go ahead and read the next section. I’ll be there waiting for you.

{mospagebreak title=Implementing the Image Search Service in PHP 5}

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.

{mospagebreak title=Implementing the Yahoo! Image Search Service with PHP 5}

If you’ve been paying attention to the approach that I followed in earlier sections of this article to implement with PHP 5 some of the most popular web services provided by Yahoo!, then it’s quite probable that you’ll find the following example very easy to grasp. It demonstrates in a friendly fashion how to use the pertinent image search service by using the same script that you learned previously.

Invoking the aforementioned Yahoo! web service through PHP requires coding only a simple PHP code snippet, like the one that follows:

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


try{

$request=’http://search.yahooapis.com/ImageSearchService/V1/imageSearch?
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 script looks very similar to the one that I used to query the Yahoo! Video Search Service, so I won’t give you boring explanations of how it works. The only thing you need to notice is how the corresponding image results are extracted from the returned serialized array, something that, at this point, should also be familiar to you. Besides, with the purpose of completing the previous explanation on using the popular Yahoo! Image Search Service with PHP 5, below I listed the pair of results produced by the prior script. Here they are:

// search results displayed by the previous script are as follows


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 pa sex for a provosere, men angrer pa boken Sex.- Jeg lurer stadig pa hva som motiverte meg til a lage den, sier hun om sin vagale bok, Sex , som vakte


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

At this stage I provided enough material for you to get started using the most relevant web services offered by Yahoo! within your own PHP 5 applications. So I recommend that you practice with these code samples to expand your skills in utilizing these handy services with PHP 5.

Final thoughts

In this third part of the series I showed you how to work with serialized PHP arrays and how to quickly parse the results returned by the Yahoo! Video and Image Search services — certainly a process that can be tackled with minor hassles.

Nonetheless, this instructive journey hasn’t ended yet, since in the next article I’m going to show you how to invoke all of those helpful web services by defining some simple, efficient custom PHP functions. This could be useful if you’re using a procedural approach to develop your PHP applications.

Now that you’ve been alerted to the topics that will be covered in the upcoming tutorial, you don’t have any excuses to miss it!

[gp-comments width="770" linklove="off" ]
antalya escort bayan antalya escort bayan