Services_Yahoo provides an object-oriented approach to interfacing with the Yahoo Web Search service (http://developer.yahoo.net/search/index.html) and Yahoo Maps. The Yahoo Web Search service includes audio, content analysis, image, local search, news, video, and Web serv- ices. It depends upon the HTTP_Request package, and although the Yahoo Web Search service requires the SimpleXML extension, working with Yahoo Maps requires DOM. The package cur- rently consists of a number of classes, although the classes pertaining to searching share a good amount of functionality and can be created through a common factory method.
■ Note At this time, the Services_Yahoo package is at version 0.1.1 and is in an alpha state. To install this package, you should use the command pear install Services_Yahoo-alpha. I won’t demonstrate Yahoo Maps in this section, because currently neither documentation nor examples exist for using the class.
I have mentioned it, however, because by the time of this book’s publication, you might be able to find either updated documentation or examples for this package.
Using Services_Yahoo_Search
The Services_Yahoo_Searchclass is simply a central point used to create the appropriate class based on the type of search to be performed. To use the search capabilities of this package, first load the class:
require_once "Services/Yahoo/Search.php";
Once included in a script, only a single function named factory()can be called. This function takes a single argument, which is a string identifying the type of search to be exe- cuted. The value can be any of the following:
web: Searches the Internet for Web pages. You can find additional information at http://
developer.yahoo.net/search/web/V1/webSearch.html.
image: Searches the Internet for images. You can find additional information at http://
developer.yahoo.net/search/image/V1/imageSearch.html.
news: Searches the Internet for news stories. You can find additional information at http://developer.yahoo.net/search/news/V1/newsSearch.html.
video: Searches the Internet for video clips. You can find additional information at http://developer.yahoo.net/search/video/V1/videoSearch.html.
local: Searches the Internet for a business near a specified location. You can find addi- tional information at http://developer.yahoo.net/search/local/V1/localSearch.html.
(Note: Currently only version 1 of the API is implemented in PEAR.)
In many examples, you may see the factory()method called statically. This is one of the areas where doing so will result in a PHP Strict Standardsmessage when running with the E_STRICTnotices enabled.
■ Note You can find additional documentation for the searches that can be performed with this class at http://developer.yahoo.net/search/index.htmlas well as in Chapter 17, where you can find examples accessing some of the Yahoo Web services using REST.
Classes returned from this function are based upon a common class and thus share a good number of methods, shown in Table 20-3. However, the classes returned for web, news, and localadd a couple of specific methods for those particular searches.
Table 20-3.Public Search Methods
Method Prototype Description
setAdultOK void setAdultOK() Allows adult content to be returned in results. This is not used in all searches.
setAppID void setAppID(string $id) Sets the application ID, which is an ID that has been registered with Yahoo.
The default value when not used is PEAR_Services_Yahoo.
setFormat void setFormat(string $format) This method sets the format parameter for a search. This method does not pertain to all search types, and the acceptable values depend upon the type of search being performed. You can find additional information within the Yahoo documentation for the specific search type.
Continued
Table 20-3.Public Search Methods
Method Prototype Description
setQuery void setQuery(string $query) Sets the query for the search.
setResultNumber void setResultNumber(int $count) Sets the number of results to return from a search. The use of this method depends upon the type of search being performed.
setStart void setStart(int $start) Sets the starting position for the first result returned. The use of this method depends upon the type of search being performed.
setType void setType(string $type) Sets the kind of search to be per- formed. This method does not pertain to all search types, and the acceptable values depend upon the type of search being performed. You can find addi- tional information within the Yahoo documentation for the specific search type.
submit (object)Services_Yahoo_Response Submits the search and returns a
submit() Services_Yahoo_Responseobject used
to handle the results.
The following example performs a Web search using the query php5 xml. This is the same query performed in Chapter 17 in Listing 17-5:
<?php
require_once "Services/Yahoo/Search.php";
try {
/* Instantiating object rather than static call to avoid E_STRICT message */
$service_yahoo = new Services_Yahoo_Search();
$search = $service_yahoo->factory("web");
$search->setQuery("php5 xml");
$search->setResultNumber(5);
$results = $search->submit();
if ($results->getTotalResultsReturned() > 0) { foreach ($results AS $info) {
print 'Title: '.$info['Title']."\n";
print 'Url: '.$info['Url']."\n";
print 'Mod Date: '.date ('M d Y', (int)$info['ModificationDate'])."\n\n";
} }
} catch (Services_Yahoo_Exception $e) { echo "Error: " . $e->getMessage() . "\n";
foreach ($e->getErrors() as $error) { echo " " . $error . "\n";
} }
?>
If you compare this code to that used in Chapter 17 with the code written using the Services_Yahoo_Searchclass, the biggest difference is that you do not need to manually create the query. Working with the results is not too much different. Rather than using SimpleXML to navigate the results, like in Chapter 17, you can use a mixture of a Services_Yahoo_Search_
Responseobject and arrays.
I don’t know about you, but in my opinion working with SimpleXML natively to parse a response from Yahoo is much simpler and cleaner than working with these classes; however, you may find that not having to deal with manually creating a query outweighs this. Your deci- sion should be based on personal preference. In any event, the resulting output looks the same as that from Chapter 17, although the individual results will vary since the scripts were not run on the same day:
Title: Zend Technologies - PHP 5 In Depth - XML in PHP 5 - What's New?
Url: http://www.zend.com/php5/articles/php5-xmlphp.php Mod Date: Nov 02 2005
Title: XML with PHP5 - encoding
Url: http://www.topxml.com/forum/m_1470/printable.htm Mod Date: Oct 18 2005
Title: Zend Technologies - PHP 5 In Depth - SimpleXML Url: http://www.zend.com/php5/articles/php5-simplexml.php Mod Date: Nov 13 2005
Title: ONLamp.com: Using PHP 5's SimpleXML
Url: http://www.onlamp.com/pub/a/php/2004/01/15/simplexml.html Mod Date: Nov 13 2005
Title: PHPBuilder.com - [Resolved] PHP5 xml_set_default_handler
Url: http://www.phpbuilder.com/board/showthread.php?s=&threadid=10272891 Mod Date: Oct 29 2005