A Few Examples - Page 7
September 11, 2002
Client
So, you now have a server that is capable of creating a WDDX packet from
the results of a database query. All you need now is a client to connect
to this server, retrieve the packet, and decode it into a native PHP array
for use on an HTML page. Listing 5.18 contains the code for this client.
Listing 5.18 A Simple WDDX Client
<?php
// client.php - read and decode WDDX packet
// this script runs at http://brutus.clientdomain.com/client.php
// url of server page
$url = "http://caesar.xtidomain.com/customers/server.php";
// probably implement some sort of authentication mechanism here
// proceed further only if client is successfully authenticated
// read WDDX packet into string
$output = join ('', file($url));
// deserialize
$cPackage = wddx_deserialize($output);
?>
<html>
<head>
<basefont face="Arial">
<!-- reload page every two minutes for latest data -->
<meta http-equiv="refresh" content="120;
URL= http://brutus.clientdomain.com/client.php">
</head>
<body>
<?
// if array contains data
if (sizeof($cPackage) > 0)
{
// format and display
?>
<table border="1" cellspacing="5" cellpadding="5">
<tr>
<td><b>Symbol</b></td>
<td><b>Price (USD)</b></td>
<td><b>Timestamp</b></td>
</tr>
<?php
// iterate through array
// key => symbol
// value = array(price, timestamp)
while (list($key, $value) = each($cPackage))
{
echo "<tr>\n";
echo "<td>$key</td>\n";
echo "<td>$value[0]</td>\n";
echo "<td>$value[1]</td>\n";
echo "</tr>\n";
}
?>
</table>
<?
}
else
{
echo "No data available";
}
?>
</body>
</html>
The client is even simpler than the server. It connects to the specified
server URL and authenticates itself. (I didn't go into the details
of the authentication mechanism to be used, but it would probably be a
host-username-password combination to be validated against XTI's
customer database.) It then reads the WDDX packet printed by the server
into an array with the file() function. This array is then converted
into a string and deserialized into a native PHP associative array with
wddx_deserialize().
After the data is decoded into a PHP associative array, a while
loop can be used to iterate through it, identifying and displaying the
important elements as a table.
Figure 5.1 shows what the resulting output
looks like.
Figure 5.1
Retrieving stock prices from a database via a WDDX-based client-server system.
A Few Examples - Page 6
XML and PHP
A Few Examples - Page 8
|