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

Serving outputs of Yahoo! Web services as serialized PHP arrays - 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.

  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



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:


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

Your-AP-ID &query=Madonna&results=2&output=php’;

// trigger the http request


throw new Exception(‘Error requesting Yahoo! Web service’);



echo ‘print_r($results,TRUE)’;


catch(Exception $e){

echo $e->getMessage();



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] =>
-/SIG=11biuqam6/EXP=1192720720/**http%3A//www.madonna.com/ [DisplayUrl]
=> www.madonna.com/ [ModificationDate] => 1192518000 [MimeType] =>
text/html [Cache] => Array ( [Url] =>
3Fei=UTF-8%26appid=Your-AP-ID %26query=Madonna%26results=2%
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;
[DisplayUrl] => www.myspace.com/madonna [ModificationDate] => 1192518000
[MimeType] => text/html [Cache] => Array ( [Url] =>
3Fei=UTF-8%26appid=Your-AP-ID %26query=Madonna%26results=2%
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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


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