Web Developer's Virtual Library: Encyclopedia of Web Design Tutorials, Articles and Discussions


WDVL Newsletter

Active Server Pages
JSP/Java Servlets
Microsoft SQL Server
Daily Backup
Dedicated Servers
Streaming Audio/Video
24-hour Support    

jobs.webdeveloper.com

Hiermenus


e-commerce
Partner With Us















Developer Channel
FlashKit.com
JavaScript.com
JavaScriptSource
Developer Jobs
ScriptSearch
StreamingMediaWorld
Web Developer's Journal
Web Developer's Virtual Library
WebDeveloper.com
Webreference
Web Hosts
XMLfiles.com

internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers


A Few Examples - Page 6

September 11, 2002

So that's the theory. There wasn't much of it, but don't let that discourage you—it's possible to build some fairly powerful distributed applications using the simple functions described in the previous sections.

Information Delivery with WDDX and HTTP

This section discusses one of the most popular applications of this technology, using it to build a primitive push/pull engine for information delivery over the web. I'll be using a MySQL database as the data source, WDDX to represent the data, and PHP to perform the actual mechanics of the transaction.

Requirements

Let's assume the existence of a fictional corporation—XTI Inc.—that plans to start up an online subscription service offering access to share market data. XTI already has access to this information via an independent source, and its database of stocks and their prices is automatically updated every few minutes with the latest market data. XTI's plan is to offer customers access to this data, allowing them to use it on their own web sites in exchange for a monthly subscription fee.

Listing 5.16 has a slice of the MySQL table that holds the data we're interested in.

Listing 5.16 A Sample Recordset from the MySQL Table Holding Stock Market Information

+--------+--------+---------------------+
| symbol | price | timestamp      |
+--------+--------+---------------------+
| DTSJ  | 78.46 | 2001-11-22 12:20:57 |
| DNDS  |  5.89 | 2001-11-22 12:32:12 |
| MDNC  | 12.94 | 2001-11-22 12:21:34 |
| CAJD  | 543.89 | 2001-11-22 12:29:01 |
| WXYZ  | 123.67 | 2001-11-22 12:28:32 |
+--------+--------+---------------------+

All that is required is an interface to this database so that subscribers to the service can connect to the system and obtain prices for all or some stocks (keyed against each stock's unique four-character symbol).

Implementing these requirements via WDDX is fairly simple and can be accomplished via two simple scripts—one for each end of the connection. A WDDX server can be used at the XTI end of the connection to accept incoming client requests and deliver formatted WDDX packets to them. At the other end of the connection, WDDX-friendly clients can read these packets, decode them, and use them in whatever manner they desire.

Server

Let's implement the server first. Listing 5.17 has the complete code.

Listing 5.17 A Simple WDDX Server

<?php
// server.php - creates WDDX packet containing symbol, 
price and timestamp of all/selected stock(s)

// this script will run at the 
	URL http://caesar.xtidomain.com/customers/server.php

// open connection to database
$connection = mysql_connect("mysql.xtidomain.com", "wddx", "secret") or die
("Unable to connect!");
mysql_select_db("db7643") or die ("Unable to select database!");

// get data
$query = "SELECT symbol, price, timestamp FROM stocks";

// if a symbol is specified, modify query to get only that record
if ($_GET['symbol'];
{
   $symbol = $_GET['symbol'];
   $query .= " WHERE symbol = '$symbol'";
}

$query .= " ORDER BY timestamp";

$result = mysql_query($query) or die 
	("Error in query: $query. " . mysql_error());

// if a result is returned
if (mysql_num_rows($result) > 0)
{

   // iterate through resultset
   while($row = mysql_fetch_row($result))
   {

      // add data to $sPackage[] associative array
      // $sPackage is an array of the form ($symbol => array($price,
	  $timestamp), ... )

      $sPackage[$row[0]] = array($row[1], $row[2]);
   }
}

// close database connection
mysql_close($connection);

// create WDDX packet
echo wddx_serialize_value($sPackage);
?>

This may appear complex, but it's actually pretty simple. Because the data is stored in a database, the first task must be to extract it using standard MySQL query functions. The returned resultset may contain either a complete list of all stocks currently in the database with their prices or a single record corresponding to a client-specified stock symbol.

Next, this data must be packaged into a form that can be used by the client. For this example, I packaged the data into an associative array named $sPackage, whose every key corresponds to a stock symbol in the table. Every key is linked to a value, which is itself a two-element array containing the price and timestamp.

After all the records in the resultset are processed, the $sPackage array is serialized into a WDDX packet with wddx_serialize_value() and then printed as output.

Decoding Data with WDDX - Page 5
XML and PHP
A Few Examples - Page 7


Up to => Home / Authoring / Languages / XML / WebServices / WDDX / XMLandPHP




Jupiter Online Media: internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and Jupiter Online Media

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Web Hosting | Newsletters | Tech Jobs | Shopping | E-mail Offers