PHP
  Home arrow PHP arrow Page 4 - Fetching Search Results as Serialized Arrays with Yahoo Web Services and PHP 5
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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? 
Google.com  
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: starstarstarstarstar / 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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    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
     

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek