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.

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

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
 

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: