PHP
  Home arrow PHP arrow Page 4 - 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 - Serving outputs of Yahoo! Web services as serialized PHP arrays
    (Page 4 of 5 )

    As I stated in the prior section, the results returned by a given Yahoo! Web Service can be retrieved in the form of a PHP serialized array by specifying an “output=php” option within the corresponding query string.

    To see in detail how this process works, study the following code sample. It first queries the Yahoo! Web Search service with the same “Madonna” search term, and then returns the results as a serialized PHP array. Here it is:


    try{

    // example using Yahoo! Web Search Service - results are displayed specifying PHP output

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

    echo ‘print_r($results,TRUE)’;

    }

    catch(Exception $e){

    echo $e->getMessage();

    exit();

    }


    In the previous example, you can see that I specified that all of the search results returned by the Yahoo! Web Search Service must be retrieved in the form of a serialized PHP array.

    In this case, the output generated by the previous script would be similar to this:


    Array ( [ResultSet] => Array ( [type] => web [totalResultsAvailable] => 65700000
    [totalResultsReturned] => 2 [firstResultPosition] => 1 [moreSearch]
    => /WebSearchService/V1/webSearch?query=Madonna&appid=Your-AP-
    ID&region=us [Result] => Array ( [0] => Array ( [Title] => Madonna [Summary] =>
    Official site of pop diva Madonna, with news, music, media, and fan club. [Url] =>
    http://www.madonna.com/ [ClickUrl] =>
    http://uk.wrs.yahoo.com/_ylt=A0Je5anQJxZH2HoAogndmMwF;
    _ylu=X3oDMTB2cXVjNTM5BGNvbG8DdwRsA1dTMQRwb3MDMQRzZWMDc3IEdnRpZAM
    -/SIG=11biuqam6/EXP=1192720720/**http%3A//www.madonna.com/ [DisplayUrl]
    => www.madonna.com/ [ModificationDate] => 1192518000 [MimeType] =>
    text/html [Cache] => Array ( [Url] =>
    http://uk.wrs.yahoo.com/_ylt=A0Je5anQJxZH2HoAowndmMwF;
    _ylu=X3oDMTBwOHA5a2tvBGNvbG8DdwRwb3MDMQRzZWMDc3IEdnRpZAM-
    /SIG=15v65vjvq/EXP=1192720720/**http%3A//66.218.69.11/search/cache%
    3Fei=UTF-8%26appid=Your-AP-ID %26query=Madonna%26results=2%
    26output=php%26u=www.madonna.com/%26w=madonna%
    26d=ZjRUGudmPoUK%26icp=1%26.intl=us [Size] => 18519 ) ) [1] => Array
    ( [Title] => MySpace.com - Madonna - Pop / Rock - www.myspace.com/madonna
    [Summary] => Madonna MySpace page with news, blog, music downloads,
    desktops, wallpapers, and more. [Url] => http://www.myspace.com/madonna
    [ClickUrl] => http://uk.wrs.yahoo.com/_ylt=A0Je5anQJxZH2HoApQndmMwF;
    _ylu=X3oDMTB2ZjQ4dDExBGNvbG8DdwRsA1dTMQRwb3MDMgRzZWMDc3IEdnRpZAM
    -/SIG=11infbg8f/EXP=1192720720/**http%3A//www.myspace.com/madonna
    [DisplayUrl] => www.myspace.com/madonna [ModificationDate] => 1192518000
    [MimeType] => text/html [Cache] => Array ( [Url] =>
    http://uk.wrs.yahoo.com/_ylt=A0Je5anQJxZH2HoApgndmMwF;
    _ylu=X3oDMTBwZG5hOWwzBGNvbG8DdwRwb3MDMgRzZWMDc3IEdnRpZAM-
    /SIG=1663fh7ed/EXP=1192720720/**http%3A//66.218.69.11/search/cache%
    3Fei=UTF-8%26appid=Your-AP-ID %26query=Madonna%26results=2%
    26output=php%26u=www.myspace.com/madonna%26w=madonna%26d=Z-awV-
    dmPogu%26icp=1%26.intl=us [Size] => 138157 ) ) ) ) )

    As you can see, all the results produced by the corresponding search term have been returned in a serialized array, which was unserialized before being outputted to the browser. Although this array is actually multidimensional, it’s quite easy to extract certain elements for further processing. Quite simple to grasp, right?

    At this point you learned not only how to implement the Yahoo! Web Search Service with PHP 5, but how to return the results as a serialized PHP array. Nevertheless, you're probably wondering how this feature can be exploited in a useful manner. Well, as you saw in the previous example, it’s fairly simple to extract some of these array elements to display them in a more readable format using some fancy markup.

    Thus, in the next section I’m going to teach you how perform this task with minor efforts, so click on the link below and keep reading.

    More PHP Articles
    More By Alejandro Gervasio


       · 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




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