How to Do Everything with Web 2.0 Mashups phần 1 - Pdf 21

Simpo PDF Merge and Split Unregistered Version -
How to Do Everything with
Web 2.0 Mashups
New York Chicago San Francisco Lisbon
London Madrid Mexico City Milan New Delhi
San Juan Seoul Singapore Sydney Toronto
Jesse Feiler
Simpo PDF Merge and Split Unregistered Version -
Copyright © 2008 by The McGraw-Hill Companies. All rights reserved. Manufactured in the United States of America. Except as permitted
under the United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or
stored in a database or retrieval system, without the prior written permission of the publisher.
0-07-159568-6
The material in this eBook also appears in the print version of this title: 0-07-149627-0.
All trademarks are trademarks of their respective owners. Rather than put a trademark symbol after every occurrence of a trademarked name,
we use names in an editorial fashion only, and to the benefit of the trademark owner, with no intention of infringement of the
trademark. Where such designations appear in this book, they have been printed with initial caps.
McGraw-Hill eBooks are available at special quantity discounts to use as premiums and sales promotions, or for use in corporate training
programs. For more information, please contact George Hoare, Special Sales, at or (212) 904-4069.
TERMS OF USE
This is a copyrighted work and The McGraw-Hill Companies, Inc. (“McGraw-Hill”) and its licensors reserve all rights in and to the work. Use
of this work is subject to these terms. Except as permitted under the Copyright Act of 1976 and the right to store and retrieve one copy of the
work, you may not decompile, disassemble, reverse engineer, reproduce, modify, create derivative works based upon, transmit,
distribute, disseminate, sell, publish or sublicense the work or any part of it without McGraw-Hill’s prior consent. You may use the work for
your own noncommercial and personal use; any other use of the work is strictly prohibited. Your right to use the work may be terminated if
you fail to comply with these terms.
THE WORK IS PROVIDED “AS IS.” McGRAW-HILL AND ITS LICENSORS MAKE NO GUARANTEES OR WARRANTIES AS TO
THE ACCURACY, ADEQUACY OR COMPLETENESS OF OR RESULTS TO BE OBTAINED FROM USING THE WORK,
INCLUDING ANY INFORMATION THAT CAN BE ACCESSED THROUGH THE WORK VIA HYPERLINK OR OTHERWISE, AND
EXPRESSLY DISCLAIM ANY WARRANTY, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO IMPLIED
WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. McGraw-Hill and its licensors do not warrant
or guarantee that the functions contained in the work will meet your requirements or that its operation will be uninterrupted or error free.

in graphics and music software, Jim makes mashups, software toys, and experimental user
interfaces using Flash, JavaScript, PHP, and other tools.
Simpo PDF Merge and Split Unregistered Version -
v
Contents at a Glance
PART I Introducing Mashups
1 Welcome to the World of Mashups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2 Understanding the Mashup World . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
PART II Learn Mashup Technologies
3 Know the Web 2.0 Mashup Rules and Design Principles . . . . . . . . . . . . . 25
4 Use XML to Structure Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
5 Use JavaScript to Script the Mashup Page . . . . . . . . . . . . . . . . . . . . . . . . 47
6 Use PHP to Perform Server-Side Scripting . . . . . . . . . . . . . . . . . . . . . . . . 61
7 Use MySQL with PHP to Retrieve Mashup Data . . . . . . . . . . . . . . . . . . . 79
8 Use RSS and Atom to Receive Data Automatically . . . . . . . . . . . . . . . . . 97
9 Use XMLHttpRequest, XML-RPC, REST, and JSON
to Retrieve Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
10 Use XHTML to Structure the Mashup Pages . . . . . . . . . . . . . . . . . . . . . . 127
PART III
Build Your Own Mashups
11 Implement a Basic Mashup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
12 Use the Google Maps API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
13 Build Mashups with the Google Maps API . . . . . . . . . . . . . . . . . . . . . . . . 179
14 Use the Amazon Web Services and Google Search APIs . . . . . . . . . . . . . 205
15 Build a Mashup to Search Amazon and Google at the Same Time . . . . . . 225
16 Use the Flickr API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
17 Build a Mashup to Search Flickr and Google at the Same Time . . . . . . . . 255
18 Use the eBay API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
19 Map the Locations of eBay Items . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291

Contents
vii
For more information about this title, click here
Simpo PDF Merge and Split Unregistered Version -
viii How to Do Everything with Web 2.0 Mashups
Use Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Server-Side Scripting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Browser-Side Scripting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Access Data with APIs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Minimize Full-Page Loading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Make Your Mashup Visible to Search Engines . . . . . . . . . . . . . . . . . . . . 32
Use Object-Oriented Programming Techniques . . . . . . . . . . . . . . . . . . . 33
Adopt Standards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
CHAPTER 4 Use XML to Structure Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Understand XML Structure and Purpose . . . . . . . . . . . . . . . . . . . . . . . . 36
Learn Basic XML Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
Create and Use XML Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Use Attributes to Identify Data Characteristics . . . . . . . . . . . . . . . . . . . . 44
Avoid Confusion by Using Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . 45
CHAPTER 5 Use JavaScript to Script the Mashup Page . . . . . . . . . . . . . . . . . . . . 47
Understand JavaScript Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
Place JavaScript Scripts in Script Elements . . . . . . . . . . . . . . . . . 49
Use Scripts from an External Source with the src Attribute . . . . . 49
Scripts Are Interpreted as They Are Encountered on the
HTML Page Unless They Are in the Head Element . . . . . . . . 50
You Can Use Semicolons to End Statements . . . . . . . . . . . . . . . . 50
Continue Quoted Text Strings with \ and Concatenation (+) . . . . 50
Use Spaces and Comments for Readability . . . . . . . . . . . . . . . . . 51
JavaScript Is Case-Sensitive . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Use Local and Global Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51

Create and Process the Query . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
Fetch the Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
Disconnect from the Database . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
Create and Load a Test Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
Create a Test Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
Load the Test Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
Delete Data from the Test Database . . . . . . . . . . . . . . . . . . . . . . . 95
CHAPTER 8 Use RSS and Atom to Receive Data Automatically . . . . . . . . . . . . . . 97
Understand Syndication, RSS, and Atom . . . . . . . . . . . . . . . . . . . . . . . . 99
RSS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
Atom . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
Categorize and Label a Feed . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
Parse a Feed with PHP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
Inside the Feed’s XML Document . . . . . . . . . . . . . . . . . . . . . . . . 104
Implement the Form to Launch the Script . . . . . . . . . . . . . . . . . . 110
Implement the PHP Script to Parse the Feed . . . . . . . . . . . . . . . . 111
Create a Search Feed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
CHAPTER 9 Use XMLHttpRequest, XML-RPC, REST, and JSON
to Retrieve Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
Understand Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
Deal with Cross-Domain Scripting Issues . . . . . . . . . . . . . . . . . . . . . . . 115
Use XMLHttpRequest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
Constructing and Sending an XMLHttpRequest . . . . . . . . . . . . . 117
Handling an XMLHttpRequest Response . . . . . . . . . . . . . . . . . . 118
Putting It Together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
Implement an XML-RPC Retrieval . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
Implement a REST Retrieval . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
Implement a JSON Retrieval . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
CHAPTER 10 Use XHTML to Structure the Mashup Pages . . . . . . . . . . . . . . . . . . . 127
Understand the Need for XHTML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128

Implement the Starting Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
CHAPTER 12 Use the Google Maps API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
Get Access to the Google Maps API . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
Create the Basic Mapping Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
Create a Map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
Identify a Location with a GLatLng . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
Use a Geocoder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
Create a Marker . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
Add Text to a Marker . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
Putting It Together in the Callback Function . . . . . . . . . . . . . . . . . . . . . 173
Wrap It Up in PHP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
CHAPTER 13 Build Mashups with the Google Maps API . . . . . . . . . . . . . . . . . . . . 179
Identify the Data and the Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
Get Access to the Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
Implement the Mashup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
Simpo PDF Merge and Split Unregistered Version -
Contents xi
Create a Campaign Contributions Mashups . . . . . . . . . . . . . . . . . . . . . . 192
Decide on Your Mashup’s Objective . . . . . . . . . . . . . . . . . . . . . . 192
Identify the Data and the Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
Get Access to the Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
Regroup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
Design the User Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
Implement the Mashup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
Implement the Starting Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
CHAPTER 14 Use the Amazon Web Services and Google Search APIs . . . . . . . . . 205
Get Access to the Amazon API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
Setting Up an Amazon Account . . . . . . . . . . . . . . . . . . . . . . . . . . 207
Setting Up an Amazon Associate Account . . . . . . . . . . . . . . . . . . 207
Setting Up an Amazon Web Services Account . . . . . . . . . . . . . . . 209

Simpo PDF Merge and Split Unregistered Version -
xii How to Do Everything with Web 2.0 Mashups
CHAPTER 17 Build a Mashup to Search Flickr and Google at the Same Time . . . 255
Decide on Your Mashup’s Objective . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
Identify the Data and the Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
Get Access to the Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
Design the User Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258
Implement the Mashup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258
Implement the Starting Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
CHAPTER 18 Use the eBay API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
Get Access to the eBay API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266
Use the API Test Tool . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
Generating the Token . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268
Making the API Call . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
Use the REST Interface for GetSearchResults . . . . . . . . . . . . . . . . . . . . 271
Define Your Token and User ID . . . . . . . . . . . . . . . . . . . . . . . . . . 272
Set Up Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
Create the Request . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
Execute the Request . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
Use the SOAP Interface for GetSearchResults . . . . . . . . . . . . . . . . . . . . 274
Define Your Token and User ID . . . . . . . . . . . . . . . . . . . . . . . . . . 274
Set Up Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274
Create the Request . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274
Execute the Request . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276
Parse the XML Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277
CHAPTER 19 Map the Locations of eBay Items . . . . . . . . . . . . . . . . . . . . . . . . . . . 279
Decide on Your Mashup’s Objective . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
Identify the Data and the Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
Get Access to the Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
Design the User Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281

will find pairs of chapters: the first introduces a specific API (eBay, Google Maps, Flickr, and so
forth), and the second builds on the technologies of Part II and the specific API to create
a mashup.
The code described in most chapters is available on the author’s Web site (www
.northcountryconsulting.com—the Downloads link is at the right of the page, just beneath the
bio), as well as on the McGraw-Hill Web site (www.mhprofessional.com, or more precisely,
The downloadable code is in
self-extracting ZIP archives and is organized by chapter. Because the examples are built from
chapter to chapter, make certain that you are using the right chapter’s code: the mashup from one
chapter will have only a fraction of the code as the same mashup in a later chapter.
Note that most of the mashup code requires you to register to use the API involved. (This is
described in the book.) The code that you download will not run until you replace placeholders
such as myAccountName with your actual account name.
Simpo PDF Merge and Split Unregistered Version -
This page intentionally left blank
Simpo PDF Merge and Split Unregistered Version -
Part I
Introducing Mashups
Simpo PDF Merge and Split Unregistered Version -
This page intentionally left blank
Simpo PDF Merge and Split Unregistered Version -
Chapter 1
Welcome to the World
of Mashups
Simpo PDF Merge and Split Unregistered Version -
4 How to Do Everything with Web 2.0 Mashups
How to . . .
■ Sell Things with Mashups
■ Provide Information with Mashups
■ Create Art with Mashups

Mashups can search a database for items that have known addresses. Figure 1-1 shows one of
the most basic of such searches: a real-estate listing service ( />site). This is a natural for mapping mashups, not only because the items being mapped are not
movable (they are houses), but also because the data are already in a searchable database. The
map has the controls you find in most of the mapping mashups: zoom controls; controls to move
the map (including sensitivity to mouse drags); and a choice of a map view, a satellite view, or a
hybrid view showing both. Markers indicate data points. In this case, you can click on a marker
to open an info window with text, as is shown for the 927 Roble Av address. You can also hover
the mouse over another marker (such as 888 Creek Drive) to display a pop-up window.
The legend in the lower-right of the map identifies two types of markers, based on when
data were last updated. Mapping APIs let you customize markers to provide yet another level of
information on a map.
FIGURE 1-1
Property Listing Maps
1
Simpo PDF Merge and Split Unregistered Version -
6 How to Do Everything with Web 2.0 Mashups
A mashup is likely to be seen by a self-selected group of people (such as those interested in
real estate in a certain area). This makes a mashup a good candidate for a service such as Google
Ads. Scrolled out of view at the bottom of the window shown in Figure 1-1 are several such ads.
The real estate business certainly predates mashups, but other opportunities for mashups
represent new businesses or new areas for an existing business to pursue. TutorLinker.com
( shown in Figure 1-2, lets you specify an area and an area of skill to find
tutors. This mashup uses the Google maps API, and it demonstrates the use of a link in an info
window.
FIGURE 1-2 TutorLinker.com
Simpo PDF Merge and Split Unregistered Version -
CHAPTER 1: Welcome to the World of Mashups 7
Figure 1-3 shows another aspect of mashups. It lets you search an area for various items,
but, as you can see from the tabs at the upper left, it will let you see listings from multiple
sources such as eBay, Wal-Mart, Amazon, and Craig’s List. Mapping, shopping, and comparative


Nhờ tải bản gốc
Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status