PEAR Articles
  Home arrow PEAR Articles arrow Page 3 - Using Web Service APIs (Amazon and Yah...
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Download TestComplete 
Forums Sitemap 
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? 
PEAR ARTICLES

Using Web Service APIs (Amazon and Yahoo!) with PEAR
By: Chris Moyer
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2008-07-23

    Table of Contents:
  • Using Web Service APIs (Amazon and Yahoo!) with PEAR
  • Services_Amazon
  • Services_Yahoo
  • Further Steps

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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


    Using Web Service APIs (Amazon and Yahoo!) with PEAR - Services_Yahoo


    (Page 3 of 4 )

    The Yahoo! API allows you to provide Yahoo! search results directly on your site. The API is fairly new, and thus the PEAR Package is in an "alpha" state. As an alternative to specifying the exact version, as we did with Amazon, we can tell PEAR that we're looking for an alpha version:


    > sudo pear -d preferred_state=alpha install Services_Yahoo

    downloading Services_Yahoo-0.2.0.tgz ...

    Starting to download Services_Yahoo-0.2.0.tgz (13,381 bytes)

    .....done: 13,381 bytes

    install ok: channel://pear.php.net/Services_Yahoo-0.2.0


    You have the package installed and no access key is required, so you are ready to make a search page. In this example, we'll create a simple box for our home page, that lists the top results for "mallard ducks."

    Instead of using the PEAR::isError() the Services_Yahoo calls throw exceptions whenever there is an error, so you will need to wrap all calls in a try/catch block. This also means that you don't need to include the PEAR.php file.


    <?php

    // This brings in the API

    require_once "Services/Yahoo/Search.php";


    print '<div id="duckbox">';


    try {

    // Create a web services object for web results

    $client = Services_Yahoo_Search::factory("web");


    // Perform the search

    $results = $client->searchFor("mallard ducks");

     

    printf('<h1>lShowing %d Mallard Duck Results</h1>',

    $results->getTotalResultsReturned());


    print '<ul>';

    foreach ($results as $result) {

    // Results is an array of associative arrays

    printf('<li><a href="%s">%s</a> - %s<p>%s</p></li>',

    $result['Url'], $result['Title'],

    $result['DisplayUrl'], $result['Summary']);

    }

    print '</ul>';


    } catch (Services_Yahoo_Exception $e) {

    // Something went wrong, here, we'll display the technical

    // error details to the user. (Normally you wouldn't do this.)

    print 'Sorry, couldn't get your duck results: ';

    print $e->getMessage() . '<br/ >';

    print join(', ', $e->getErrors());


    }

    print '</div>';

    ?>


    The entire process is very simple, with the API returning an array of results. Each result is an associative array with various keys, including URL, Title and Summary.

    When you instantiate the search client, you specify what type of search you want to run. Above, we searched the web - other options are "news," "images," "video" and "local." You simply pass one of these options to the factory() method.

    The Services_Yahoo package also provides an interface to Yahoo's content analysis services. Two of these services are term extraction and spelling suggestions.

    Term extraction takes a chunk of text, and returns significant words or phrases found within the text. You might use this to index forum posts, without indexing every word. You would pass the body of the post to the term extraction API, and then catalog the returned terms in a table mapping the terms to the ID of that forum post.

    require_once "Services/Yahoo/ContentAnalysis.php";


    function indexPost($post_id, $body_text) {

    try {

    // Create the client

    $yahoo = Services_Yahoo_ContentAnalysis::factory("termExtraction");


    // Tell it what we're indexing

    $yahoo->setContext($body_text);


    $results = $yahoo->submit();


    // Store each of the key terms

    foreach ($results as $term) {

    mysql_query( sprintf('INSERT INTO post_terms (post_id, term) VALUES("%d",
    "%S")',

    $post_id,

    mysql_escape_string($term)));

    }


    } catch (Services_Yahoo_Exception $e) {

    return FALSE;

    }

    return TRUE;

    }


    Spelling suggestions are equally simple. One result is always returned. The general use for this would be the "Did you mean ____" helpers you'll see on many web pages.


    <?php

    require_once "Services/Yahoo/ContentAnalysis.php";


    $suggestion = FALSE;

    try {

    // Create the client

    $yahoo = Services_Yahoo_ContentAnalysis::factory("spellingSuggestion");


    // Tell it what word we want

    $yahoo->setQuery('alphabet');


    $results = $yahoo->submit();


    // We still loop over the results, even though there is only

    // one, as the result is an iterator

    foreach ($results as $term) {

    $suggestion = $term;

    }


    } catch (Services_Yahoo_Exception $e) {

    // print $e->getMessage();

    $suggestion = FALSE;

    }


    if ($suggestion !== FALSE) {

    printf('Did you mean %s?', $suggestion);

    } ?>


    More PEAR Articles Articles
    More By Chris Moyer


     

    PEAR ARTICLES ARTICLES

    - Deleting Authors from a PEAR Content Managem...
    - PEAR CMS: Index and Delete Scripts
    - Listing Articles for a PEAR Content Manageme...
    - Building an Authors Page for a PEAR CMS
    - Building the View Details Page in a PEAR CMS
    - Creating the Main Pages of a PEAR CMS
    - Completing the Login Script for a PEAR CMS
    - User Authentication for a PEAR CMS
    - A PEAR CMS: Examining the Code
    - Building a Content Management System with PE...
    - Installing a PEAR Package
    - My PEAR: The Beginning
    - Using XML_RPC2 with PEAR
    - Using Web Service APIs (Amazon and Yahoo!) w...
    - Database Abstraction with MDB2 from PEAR





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    Stay green...Green IT