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 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


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, & Permissions, Privacy Policy.

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