A Few Examples - Page 8
September 11, 2002
The beauty of this system is that the server and connecting clients are
relatively independent of each other. As long as a client has the relevant
permissions, and understands how to connect to the server and read the
WDDX packet returned by it, it can massage and format the data per its
own special requirements. To illustrate this, consider Listing 5.19, which
demonstrates an alternative clientthis one performing a "search"
on the server for a user-specified stock symbol.
Listing 5.19 An Alternative WDDX Client
<?php
// client.php - read and decode WDDX packet
// this script runs at http://brutus.clientdomain.com/client.php
if(!$_POST['submit'])
{
?>
<!-- search page -->
<!-- lots of HTML layout code - snipped out -->
<form action="<? echo $_SERVER['PHP_SELF']; ?>" method="post">
Enter stock symbol:
<input type="text" name="symbol" size="4" maxlength="4">
<input type="submit" name="submit" value="Search">
</form>
<?
}
else
{
// perform a few error checks
// sanitize search term
// query server with symbol as parameter
$symbol = $_POST['symbol'];
$url = "http://caesar.xtidomain.com/customers/server.php?symbol=$symbol";
// 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);
// if any data in array
if (sizeof($cPackage) > 0)
{
// format and display
list($key, $value) = each($cPackage);
echo "Current price for symbol $key is $value[0]";
}
else
{
echo "No data available";
}
}
?>
This script consists of two parts:
-
The search form itself, which contains a text box for user input
-
The form processor, which connects to the content server with the
user-specified stock symbol as parameter and massages the resulting
WDDX output into easily readable HTML
Again, even though the two clients operate in two different ways (one
displays a complete list of items, whereas the other uses a search term
to filter down to one specific item), no change was required to the server
or to the formatting of the WDDX packet.
Perl of Wisdom
It's not necessary that the WDDX clients described in Listings 5.18
and 5.19 be written in PHP. As discussed previously in this chapter, WDDX
creates platform-independent data structures that can then be deserialized
into native structures on the target platform. Consequently, it's possible
for a WDDX client written in Perl or Python to connect to a WDDX server written
in PHP, receive WDDX-compliant data packets, and use them within a script
or program.
As an illustration, consider the following Perl port (see Listing 5.20)
of the client described in Listing 5.18.
Listing 5.20 A Perl WDDX Client
#!/usr/bin/perl
# need this to read HTTP response
use LWP::UserAgent;
# need this to deserialize WDDX packets
use WDDX;
# instantiate client, connect and read response
$client = LWP::UserAgent->new;
my $req = HTTP::Request->new(GET =>
'http://caesar.xtidomain.com/ _customers/server.php');
my $res = $client->request($req);
# response is good...
if ($res->is_success)
{
# deserialize resulting packet as hash and get reference
my $wddx = new WDDX;
$packet = $wddx->deserialize($res->content);
$hashref = $packet->as_hashref;
# get a list of keys (stock symbols) within the hash
@keys = $packet->keys();
# iterate through hash
foreach $key (@keys)
{
# get a reference to the array [price, timestamp]
for each key
$arrayref = $$hashref{$key};
# print data in colon-separated format
print "$key:$$arrayref[0]:$$arrayref[1]\n";
}
}
# response is bad...
else
{
print "Error: bad connection!\n";
}
In this case, I used Perl's libwww module to connect to the WDDX
server and read the resulting packet, and the WDDX module to deserialize
it into a Perl hash reference. After the data is converted into a Perl-compliant
structure, accessing and displaying the various elements is a snap.
Perl's WDDX.pm module is far more powerful than the WDDX module
that ships with PHP 4.0, offering a wide array of different methods
to simplify access to arrays, hashes, and recordsets. (Recordsets are
not supported by PHP's WDDX module as of this writing, although
support might become available in future versions.)
More information on Perl's WDDX.pm module is available at http://www.scripted.com/wddx/.
A Few Examples - Page 7
XML and PHP
Remote Software Updates with WDDX and Socket Communication - Page 9
|