Datasheet
The $HTTP_RAW_POST_DATA variable has been initialized to contain the data that is received by HTTP
POST methods. In that case, it contains the XML document that was sent by the browser, and its size is
obviously greater than zero, which causes the
write() function to be called:
wwaattcchh..pphhpp –– vv11..00
function write() {
global $HTTP_RAW_POST_DATA;
$db=openDb();
$dom = new DOMDocument();
$dom->loadXML($HTTP_RAW_POST_DATA);
if (!$dom->relaxNGValidate ( ‘watch.rng’)) {
die(“unvalid document”);
}
$xml = simplexml_import_dom($dom);
foreach ($xml->children() as $element) {
$element[‘escaped’] = sqlite_escape_string(trim($element));
}
//echo $xml->asXML();
$query = $db->query(
“SELECT symbol from watches where symbol=’”.
$xml->symbol[‘escaped’].
“‘“,
SQLITE_NUM);
$req = “”;
if ($query->fetch()) {
$req=”update watches set “;
$req .= “tag=’”.$xml->tag[‘escaped’].”’, “;
$req .= “title=’”.$xml->title[‘escaped’].”’, “;
$req .= “description=’”.$xml->description[‘escaped’].”’ “;
$req .= “where symbol=’”.$xml->symbol[‘escaped’].”’”;
} else {
$req=”insert into watches (symbol, tag, title, description) values (“;
$req .= “‘“.$xml->symbol[‘escaped’].”’, “;
$req .= “‘“.$xml->tag[‘escaped’].”’, “;
$req .= “‘“.$xml->title[‘escaped’].”’, “;
$req .= “‘“.$xml->description[‘escaped’].”’)”;
}
//echo $req;
$db->queryExec($req);
echo “<ok/>”;
}
BuzzWatch, the sample application for this chapter comes in four different versions packaged in four
different archives. The file references include the file name and the version.
The first instruction is as usual to open and create the database if necessary. The next ones are to handle
the XML document that has been received. BuzzWatch could have read the XML document directly with
SimpleXML. Instead, it has been decided that some tests need to be done to check that the document is
conformant to what is expected. BuzzWatch could have done these tests in PHP. However, because the
document is in XML, a more concise option is to rely on an XML schema language.
22
Chapter 1
04_087889 ch01.qxp 5/2/07 12:56 PM Page 22