Examples

Examples – Using Services_Yahoo

About the examples

The following sections provide you with examples of using the different feature sets of Services_Yahoo. Currently this includes documentation for the interfaces to Yahoo! Search and Content Analysis.

All public methods in Services_Yahoo have in common that exceptions will be raised when something goes wrong. This is why try { ... } catch { } blocks are wrapped around all examples.

The examples are designed to be run from a command line shell. If you would like to test them in a web browser you should replace \n with <br /> for better readability.

Speaking to Yahoo! Search

The following examples will communicate with Yahoo! Search.

Web search: Listing results

This snippet issues a search query for the term Steve Fossett to Yahoo! Search. For each result in the returned result set the title is printed.

<?php
require_once "Services/Yahoo/Search.php";

try {
    
$client Services_Yahoo_Search::factory("web");
    
$results $client->searchFor("Steve Fossett");

    echo 
"Number of results: " $results->getTotalResultsReturned() . "\n\n";

    foreach (
$results as $result) {
        echo 
$result['Title'] . "\n";
    }

} catch (
Services_Yahoo_Exception $e) {
    echo 
"Error: " $e->getMessage() . "\n";

    foreach (
$e->getErrors() as $error) {
        echo 
"* " $error "\n";
    }
}
?>

By default 10 results are returned per request. This number can be modified using the method setResultNumber():

<?php
require_once "Services/Yahoo/Search.php";

try {
    
$client Services_Yahoo_Search::factory("web");

    
// Get 20 results per query
    
$results $client->withResults(20)->searchFor("Steve Fossett");

    
/* ... */
?>

Web search: Result details

This code again queries Yahoo! Search for Steve Fossett, but this time the details of the first result in the result set are printed.

<?php
require_once "Services/Yahoo/Search.php";

try {
    
$client Services_Yahoo_Search::factory("web");
    
$results $client->searchFor("Steve Fossett");

    if (
$results->getTotalResultsReturned() > 0) {
        
$info $results->current();

        echo 
"Title: " $info['Title'] . "\n";
        echo 
"Summary: " $info['Summary'] . "\n";
        echo 
"URL: " $info['Url'] . "\n";
        echo 
"clickable URL: " $info['ClickUrl'] . "\n";
        echo 
"Modification date: " $info['ModificationDate'] . "\n";
        echo 
"Mime type: " $info['MimeType'] . "\n";
    }
    
} catch (
Services_Yahoo_Exception $e) {
    echo 
"Error: " $e->getMessage() . "\n";

    foreach (
$e->getErrors() as $error) {
        echo 
"* " $error "\n";
    }
}
?>

Web search: Paginating results

In this example the "paginating" capabilities of Services_Yahoo are shown. Paginating means that the search results are split up into chunks of e.g. 20, which are displayed together with links to jump back and forth to other result chunks. Basically this mimicks the functionality from the bottom of official Yahoo Search Page.

TBD

In order to query the Image, News, Video or Local search, one only needs to replace the argument "web" in the call of the factory() method with one of "image", "news", "video", or "local".

Using the Yahoo! Content Analysis services

The following examples will show you how to use the Content Analysis Services provided by Yahoo!.

Term Extraction Service

The Term Extraction service provides a list of significant words or phrases extracted from a larger content.

<?php
require_once "Services/Yahoo/ContentAnalysis.php";

try {
    
$search Services_Yahoo_ContentAnalysis::factory("termExtraction");
    
$search->setContext("Italian sculptors and painters of the "
        
"renaissance favored the Virgin Mary for inspiration.");
    
$search->setQuery("madonna");

    
$results $search->submit();

    foreach (
$results as $result) {
        echo 
$result "\n";
    }

} catch (
Services_Yahoo_Exception $e) {
    echo 
"Error: " $e->getMessage() . "\n";

    foreach (
$e->getErrors() as $error) {
        echo 
"* " $error "\n";
    }
}
?>

It is possible to skip the call to setQuery(). The parameter set by this method is intended to help the engine with the extraction process, but it is not stricly required.

Spelling Suggestion Service

The Spelling Suggestion service provides a suggested spelling correction for a given term.

The following code queries Yahoo for a spelling suggestion for the term "madnna". The service will return exactly one result, but currently there is no way to avoid looping through the $results.

<?php
require_once "Services/Yahoo/ContentAnalysis.php";

try {
    
$search Services_Yahoo_ContentAnalysis::factory("spellingSuggestion");
    
$search->setQuery("madnna");

    
$results $search->submit();

    foreach (
$results as $result) {
        echo 
$result "\n";
    }
} catch (
Services_Yahoo_Exception $e) {
    echo 
"Error: " $e->getMessage() . "\n";

    foreach (
$e->getErrors() as $error) {
        echo 
"* " $error "\n";
    }
}
?>
Introduction to Services_Yahoo (Previous) Services_Yahoo's design (Next)
Last updated: Fri, 19 Dec 2014 — Download Documentation
Do you think that something on this page is wrong? Please file a bug report or add a note.
View this page in:

User Notes:

There are no user contributed notes for this page.