PHP
  Home arrow PHP arrow Page 5 - Fetching Search Results as Serialized ...
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Fetching Search Results as Serialized Arrays with Yahoo Web Services and PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2008-01-15

    Table of Contents:
  • Fetching Search Results as Serialized Arrays with Yahoo Web Services and PHP 5
  • Displaying web services search results in XML format
  • Next Example
  • Serving outputs of Yahoo! Web services as serialized PHP arrays
  • Iterating over array elements with PHP 5

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
     
    ADVERTISEMENT

    Route your faxes to your email inbox. Private, secure fax numbers available from CallWave. Choose your fax number.

    Fetching Search Results as Serialized Arrays with Yahoo Web Services and PHP 5 - Iterating over array elements with PHP 5
    (Page 5 of 5 )

    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!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · Over the course of this second chapter of the series, you’ll learn how to fetch the...
     

       

    PHP ARTICLES

    - Setting Up a Web-based Image Hosting Service
    - Comparing Files and Databases with PHP Bench...
    - Setting Up a Web-Based Image Gallery
    - Using Timers to Benchmark PHP Applications
    - Benchmarking Applications with PHP
    - Setting Up a Web-Based File Manager: PHPfile...
    - Developing a Modular Class For a PHP File Up...
    - Setting Up a Web-Based File Manager: bfExplo...
    - Defining a Custom Function for File Uploader...
    - Parsing Child Nodes with the DOM XML extensi...
    - Creating an Error Handling Module for a PHP ...
    - Accessing Attributes and Cloning Nodes with ...
    - Retrieving Information on Selected Files wit...
    - Handling HTML Strings and Files with the DOM...
    - Building File Uploaders with PHP 5

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway