Datasheet

This restriction is known as the same origin policy. In a nutshell, it means that a script served from a
domain can only access resources from the same domain. In Chapter 15, you will see that this is a gen-
eral issue for mashups and that data providers such as Google Maps and Yahoo! Maps have worked
around the limitation by serving the scripts that decorate the maps from their own domain. If the script
that downloads the del.icio.us RSS feed was served by del.icio.us, the script and the feed would belong to
the same domain, and the same origin policy would not be violated. Unfortunately, this workaround
requires that the data provider has anticipated the need, which is not often the case.
Because a script executed served from a domain can access only resources from this domain, you have to
use a proxy to access the sources that you want to aggregate. A generic proxy like Apache’s mod-proxy
can be used, but implementing your own caching proxy as done by BuzzWatch is also an opportunity to
change the format of the documents that are being served. For example, BuzzWatch is using stock quotes
delivered by Yahoo! as CSV (comma-separated values) documents, and its caching proxy does the con-
version from CSV to XML. Converting non-XML data into XML is a very common pattern for Web 2.0
(covered in Chapter 13). You’ve already seen an example of such a conversion with SQL accesses, and
most of the time these conversions are quite straightforward. The conversion from CSV to XML uses
SimpleXML and a regular expression; it is located in
yahoo_quotes.php and is as simple as this:
function get_xml($url) {
$csv = file_get_contents($url, “r”);
$a = ‘“([^”]*)”’;
$n = ‘([^,]*)’;
$pattern = “/^$a,$a,$n,$a,$a,$n,$n,$n,$n,$n$/”;
$match = preg_match($pattern, $csv, $matches);
$xml = new SimpleXMLElement(
“<quote><symbol/><name/><lastTrade><price/><date/><time/>”.
“</lastTrade><change/><open/><high/><low/><volume/></quote>”);
$xml->symbol = $matches[1];
$xml->name = $matches[2];
$xml->lastTrade->price = $matches[3];
$xml->lastTrade->date = $matches[4];
$xml->lastTrade->time = $matches[5];
$xml->change = $matches[6];
$xml->open = $matches[7];
$xml->high = $matches[8];
$xml->low = $matches[9];
$xml->volume = $matches[10];
return $xml->asXML();
}
The second question was to see how documents can be saved on the server. The exchange is logged as:
10:18:43 200 POST /buzzwatch/watch.php (application/xml)
It is initiated by the JavaScript method attached to the Save menu item in controller.js:
BuzzWatchController.prototype.save = function() {
var xotree = new XML.ObjTree();
var tree = {
watch: {
symbol: YAHOO.buzzWatch.config.symbol,
tag: YAHOO.buzzWatch.config.tag,
title: YAHOO.buzzWatch.editInPlace.get(‘textTitle’),
20
Chapter 1
04_087889 ch01.qxp 5/2/07 12:56 PM Page 20