- XML Reference Guide
- Overview
- What Is XML?
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Table of Contents
- The Document Object Model
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- DOM and Java
- Informit Articles and Sample Chapters
- Books and e-Books
- Implementations
- DOM and JavaScript
- Using a Repeater
- Repeaters and XML
- Repeater Resources
- DOM and .NET
- Informit Articles and Sample Chapters
- Books and e-Books
- Documentation and Downloads
- DOM and C++
- DOM and C++ Resources
- DOM and Perl
- DOM and Perl Resources
- DOM and PHP
- DOM and PHP Resources
- DOM Level 3
- DOM Level 3 Core
- DOM Level 3 Load and Save
- DOM Level 3 XPath
- DOM Level 3 Validation
- Informit Articles and Sample Chapters
- Books and e-Books
- Documentation and Implementations
- The Simple API for XML (SAX)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- SAX and Java
- Informit Articles and Sample Chapters
- Books and e-Books
- SAX and .NET
- Informit Articles and Sample Chapters
- SAX and Perl
- SAX and Perl Resources
- SAX and PHP
- SAX and PHP Resources
- Validation
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Document Type Definitions (DTDs)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XML Schemas
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- RELAX NG
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Schematron
- Official Documentation and Implementations
- Validation in Applications
- Informit Articles and Sample Chapters
- Books and e-Books
- XSL Transformations (XSLT)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XSLT in Java
- Java in XSLT Resources
- XSLT and RSS in .NET
- XSLT and RSS in .NET Resources
- XSL-FO
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XPath
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XML Base
- Informit Articles and Sample Chapters
- Official Documentation
- XHTML
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XHTML 2.0
- Documentation
- Cascading Style Sheets
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XUL
- XUL References
- XML Events
- XML Events Resources
- XML Data Binding
- Informit Articles and Sample Chapters
- Books and e-Books
- Specifications
- Implementations
- XML and Databases
- Informit Articles and Sample Chapters
- Books and e-Books
- Online Resources
- Official Documentation
- SQL Server and FOR XML
- Informit Articles and Sample Chapters
- Books and e-Books
- Documentation and Implementations
- Service Oriented Architecture
- Web Services
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Creating a Perl Web Service Client
- SOAP::Lite
- Amazon Web Services
- Creating the Movable Type Plug-in
- Perl, Amazon, and Movable Type Resources
- Apache Axis2
- REST
- REST Resources
- SOAP
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- SOAP and Java
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- WSDL
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- UDDI
- UDDI Resources
- XML-RPC
- XML-RPC in PHP
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Ajax
- Asynchronous Javascript
- Client-side XSLT
- SAJAX and PHP
- Ajax Resources
- JSON
- Ruby on Rails
- Creating Objects
- Ruby Basics: Arrays and Other Sundry Bits
- Ruby Basics: Iterators and Persistence
- Starting on the Rails
- Rails and Databases
- Rails: Ajax and Partials
- Rails Resources
- Web Services Security
- Web Services Security Resources
- SAML
- Informit Articles and Sample Chapters
- Books and e-Books
- Specification and Implementation
- XML Digital Signatures
- XML Digital Signatures Resources
- XML Key Management Services
- Resources for XML Key Management Services
- Internationalization
- Resources
- Grid Computing
- Grid Resources
- Web Services Resource Framework
- Web Services Resource Framework Resources
- WS-Addressing
- WS-Addressing Resources
- WS-Notifications
- New Languages: XML in Use
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Google Web Toolkit
- GWT Basic Interactivity
- Google Sitemaps
- Google Sitemaps Resources
- Accessibility
- Web Accessibility
- XML Accessibility
- Accessibility Resources
- The Semantic Web
- Defining a New Ontology
- OWL: Web Ontology Language
- Semantic Web Resources
- Google Base
- Microformats
- StructuredBlogging
- Live Clipboard
- WML
- XHTML-MP
- WML Resources
- Google Web Services
- Google Web Services API
- Google Web Services Resources
- The Yahoo! Web Services Interface
- Yahoo! Web Services and PHP
- Yahoo! Web Services Resources
- eBay REST API
- WordML
- WordML Part 2: Lists
- WordML Part 3: Tables
- WordML Resources
- DocBook
- Articles
- Books and e-Books
- Official Documentation and Implementations
- XML Query
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XForms
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Resource Description Framework (RDF)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Topic Maps
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation, Implementations, and Other Resources
- Rich Site Summary (RSS)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Simple Sharing Extensions (SSE)
- Atom
- Podcasting
- Podcasting Resources
- Scalable Vector Graphics (SVG)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- OPML
- OPML Resources
- Summary
- Projects
- JavaScript TimeTracker: JSON and PHP
- The Javascript Timetracker
- Refactoring to Javascript Objects
- Creating the Yahoo! Widget
- Web Mashup
- Google Maps
- Indeed Mashup
- Mashup Part 3: Putting It All Together
- Additional Resources
- Frequently Asked Questions About XML
- What's XML, and why should I use it?
- What's a well-formed document?
- What's the difference between XML and HTML?
- What's the difference between HTML and XHTML?
- Can I use XML in a browser?
- Should I use elements or attributes for my document?
- What's a namespace?
- Where can I get an XML parser?
- What's the difference between a well-formed document and a valid document?
- What's a validating parser?
- Should I use DOM or SAX for my application?
- How can I stop a SAX parser before it has parsed the entire document?
- 2005 Predictions
- 2006 Predictions
- Nick's Book Picks
Now that we've discussed just what it is we can do with Yahoo! Web Services, it's time to actually do it. In this guide section, we'll build, from scratch, a PHP form that builds a Yahoo! query, sends it off, and displays the results based on the type of search.
Our intent is to build a small "module," possibly intended to be displayed on a second page, that displays the top 10 results for a given keyword. To keep things simple, we won't cover local searches, but the ideas are the same.
We'll start with a form that takes the keyword and service information:
<h3>Yahoo! Results</h3> <form action="yahoo.php" method="get"> Choose a keyword: <input type="text" name="keyword" /><br /> Choose a service: <select name="service"> <option value="web">web</option> <option value="image">images</option> <option value="video">video</option> <option value="news">news</option> </select> <input type="submit" /> </form>
The form creates a page much like figure 1:
When we submit the form, the first thing we need to do is extract the submitted information:
<?php $keyword=$HTTP_GET_VARS["keyword"]; if ($keyword == ""){ ?> <h3>Yahoo! Results</h3> <form action="yahoo.php" method="get"> Choose a keyword: <input type="text" name="keyword" /><br /> Choose a service: <select name="service"> <option value="web">web</option> <option value="image">images</option> <option value="video">video</option> <option value="news">news</option> </select> <input type="submit" /> </form> <?php } else { $searchService = $HTTP_GET_VARS["service"]; if ($searchService == "") { $searchService = "web"; } } ?>
First, we're checking to see whether the form's been submitted, based on whether or not there's a keyword
value. If not, we're displaying the form. If so, we're getting the service
value. Just to be on the safe side, if one wasn't submitted, we'll use the web
search service.
Next we need to build the URL for the request. To do that, we need to take into account that each service has its own "base" URL:
<?php function getRequestURL($baseURL, $keyword){ $otherArgs="&appid=InformITXML&results=10"; return $baseURL."?query=".$keyword.$otherArgs; } $keyword=$HTTP_GET_VARS["keyword"]; if ($keyword == ""){ ... } else { $searchService = $HTTP_GET_VARS["service"]; if ($searchService == "") { $searchService = "web"; } $baseURLs = array( "web" => "http://api.search.yahoo.com/WebSearchService/V1/webSearch", "video" => "http://api.search.yahoo.com/VideoSearchService/V1/videoSearch", "news" => "http://api.search.yahoo.com/NewsSearchService/V1/newsSearch", "image" => "http://api.search.yahoo.com/ImageSearchService/V1/imageSearch"); $fullURL = getRequestURL($baseURLs[$searchService], $keyword); }
Let's start at the bottom. The actual request consists of a URL. The URL is made up of a "base" url and various parameters. We can pull that base URL from an array based on the search service. From there we move up to the getRequestURL()
function.
The getRequestURL()
function builds the complete URL based on the base URL, which we're passing in, the query, or keyword, which we're also passing in, and any additional arguments. In this case, we're passing only the (required) appid
and the results
parameter, specifying that we only want 10 results. If we were including the Local Search service in our form, we would also have to accomodate the radius
and other location-based parameters. Finally, we combine them all and send back the results.
Now we're ready to actually send the request. Fortunately, PHP's Document Object Model implementation makes that part easy:
... $fullURL = getRequestURL($baseURLs[$searchService], $keyword); $document = domxml_open_file($fullURL); $root = $document->document_element(); $totalResults = 0; $thisPageResults = 0; if ($root->has_attributes()) { $attributesArray = $root->attributes(); foreach ($attributesArray AS $thisAttribute) { $attributeName = $thisAttribute->name(); if ($attributeName == "totalResultsAvailable") { $totalResults = $thisAttribute->value(); } elseif ($attributeName == "totalResultsReturned"){ $thisPageResults = $thisAttribute->value(); } } } echo ("<h3>Top ".$thisPageResults." Yahoo! results for \"".$keyword."\"</h3>"); echo ("<p>Total Results: ".$totalResults."</p>"); }
PHP's DOM implementation let's us parse a document at an external URL, so we don't need to fool around with making and reading HTTP requests. Instead, we get a reference to the root element, in this case ResultSet
. From there, we'll extract the totalResultsAvailable
and totalResultsReturned
attributes of the ResultSet
element so we know whether there are any results to analyze.
Assuming there are, we'll handle that next:
... echo ("<h3>Top ".$thisPageResults." Yahoo! results for \"".$keyword."\"</h3>"); echo ("<p>Total Results: ".$totalResults."</p>"); $thisResult = $root->first_child(); while ($thisResult){ $titleElement = $thisResult->get_elements_by_tagname("Title"); $titleValue = $titleElement[0]->first_child(); $title = $titleValue->node_value(); $click_urlElement = $thisResult->get_elements_by_tagname("ClickUrl"); $click_urlValue = $click_urlElement[0]->first_child(); $click_url = $click_urlValue->node_value(); $display_urlElement = $thisResult->get_elements_by_tagname("Url"); $display_urlValue = $display_urlElement[0]->first_child(); $display_url = $display_urlValue->node_value(); $summaryElement = $thisResult->get_elements_by_tagname("Summary"); $summaryValue = $summaryElement[0]->first_child(); if ($summaryValue){ $summary = $summaryValue->node_value(); } ... $thisResult = $thisResult->next_sibling(); } }
The actual results come back as Result
element children of the ResultSet
element, so we'll start by getting the first child of the ResultSet
element and analyzing it to pull the Title
, ClickUrl
(the URL the user should be directed to), Url
(the actual URL of the resource, and not a Yahoo! redirect), and the Summary
. Some results don't actually have a summary, so we'll check that before we try to access the value.
Finally, we'll move to the next sibling so we don't create an infinite loop; when we run out of children, the while()
loop stops.
Once we have the information, we can display it on the page:
... $summaryValue = $summaryElement[0]->first_child(); if ($summaryValue){ $summary = $summaryValue->node_value(); } if ($searchService == "image"){ $thumbnailElement = $thisResult->get_elements_by_tagname("Thumbnail"); $thumbnailURLElement = $thumbnailElement[0]->first_child(); $thumbnailURLValue = $thumbnailURLElement->first_child(); $thumbnail = $thumbnailURLValue->node_value(); echo ("<a href=\"".$click_url."\">"); echo ("<img src=\"".$thumbnail."\" />"); echo ("</a><br /><br />"); } else { echo ($title); echo ("<br />"); echo ("<a href=\"".$click_url."\">"); echo ($display_url); echo ("</a><br />"); echo ($summary."<br /><br />"); } $thisResult = $thisResult->next_sibling(); } }
How we actually display the information depends on what it is. If we're dealing with an Image Search, we'll want to display the thumbnail, which also means extracting extra information from the Thumbnail
element. Otherwise, we're simply displaying the title, URL (both the click and display variety) and the summary.
The results look like Figures 2 and 3:
The implementation in other languages will, of course, be different, but the concept is the same: get the query information, build the URL for the request, and pull the important information out of the results.