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

Using the Yahoo! Web Search Service with 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 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.



 
 
>>> 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: