Home arrow PHP arrow Page 5 - Fetching Search Results as Serialized Arrays with Yahoo Web Services and PHP 5

Iterating over array elements with PHP 5 - PHP

In this second article of the series dealing with the implementation of some of the most useful web services offered by Yahoo! with PHP 5, I’ll show you how to parse the results returned by a determined web search service using a few array PHP processing functions.

TABLE OF CONTENTS:
  1. Fetching Search Results as Serialized Arrays with Yahoo Web Services and PHP 5
  2. Displaying web services search results in XML format
  3. Next Example
  4. Serving outputs of Yahoo! Web services as serialized PHP arrays
  5. Iterating over array elements with PHP 5
By: Alejandro Gervasio
Rating: starstarstarstarstar / 1
January 15, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

In accordance with the concepts that I deployed in the section you just read, it’s perfectly possible to display the results returned by the Yahoo! Web Search Service by using only a few array processing PHP functions, instead of showing monolithic chunks of XML data.

To demonstrate how this process can be achieved in a simple way, below I coded another basic PHP 5 script that displays these search results by using a few (X)HTML formatting tags. The corresponding code sample is as follows:

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

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();

}


First of all, the Yahoo! Web Search Service is queried by using the same “Madonna” search term that I used earlier. And lastly, the results are returned in the form of a serialized array, since the value “php” has been assigned to the pertinent “output” parameter.

Next, I simply used a “foreach” loop to iterate over the “$result” array and extract only the elements that I want to display on the browser, such as the ones whose keys are identified as “Title,” “Summary,” and “Url.”

The unformatted output of the previous script can be seen below:


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


This is much simpler to read and code! At this point, I've provided you with an illustrative example of how to display, in a fancy format, the results returned by the Yahoo! Web Search Service. As you saw earlier, this process is very simple to grasp, so I recommend that you develop your own testing examples to extend your background in using these helpful web services with PHP 5.

Final thoughts

In this second article of the series, you hopefully learned the key concepts surrounding the implementation of the Yahoo! Web Search Service by using its useful “output=php” option. Indeed, when this parameter is correctly specified, displaying the corresponding search results in (X)HTML format is a matter of iterating over the elements of the pertinent output array and extracting only the ones that are relevant to your PHP application.

In the next part of the series, I’m going to show you how to format search results produced by the Yahoo! Video and Image Search Services in (X)HTML, so you don’t have an excuse 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: