Home arrow PHP arrow Page 8 - Data Exchange with XML, WDDX and PHP

Money Talks - PHP

You may never have heard of it before - but if your job involvesexchanging data between different servers and applications, you're going tobe hearing a lot about it very soon. Find out how WDDX makes it possible tocreate and transmit platform-neutral data structures across the Web, andhow it can be combined with PHP to create a whole new generation of Webapplications (including new content syndication and financial updateservices).

TABLE OF CONTENTS:
  1. Data Exchange with XML, WDDX and PHP
  2. The Wonderful World Of WDDX
  3. Polly Wants A Cracker
  4. Humbert Redfinch-Northbottom The Third, I Presume?
  5. Old Friends And New
  6. Hip To Be Square
  7. The Truth Is Out There
  8. Money Talks
  9. Closing Time
By: Vikram Vaswani, (c) Melonfire
Rating: starstarstarstarstar / 4
September 13, 2001

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
Let's try another example, this one using WDDX to transmit five-day currency rate data to a requesting client. The client then uses this data to calculate an average rate for the past five days.

Let's assume that the data is stored in the following database table:

mysql> select * from currency; +--------+-------+-------+-------+-------+-------+ | symbol | mark1 | mark2 | mark3 | mark4 | mark5 | +--------+-------+-------+-------+-------+-------+ | GBP | 0.72 | 0.69 | 0.73 | 0.75 | 0.69 | | INR | 0.023 | 0.045 | 0.012 | 0.019 | 0.025 | | DZD | 0.01 | 0.009 | 0.015 | 0.011 | 0.01 | | CAD | 0.66 | 0.65 | 0.68 | 0.7 | 0.64 | | DEM | 0.43 | 0.44 | 0.43 | 0.42 | 0.42 | +--------+-------+-------+-------+-------+-------+ 5 rows in set (0.00 sec)
The server script needs to get all five values and encode them as a WDDX packet.

<? // server.php - output WDDX packet containing currency data// database parameters$hostname = "medusa";$user = "wddx_agent";$pass = "jser745mf";$database = "trends";// open connection to database$connection = mysql_connect($hostname, $user, $pass) or die ("Unable toconnect!");// get rates for last five days$query = "SELECT mark1, mark2, mark3, mark4, mark5 FROM currency WHEREsymbol = '$symbol'";$result = mysql_db_query($database, $query, $connection) or die ("Error inquery: $query. " . mysql_error());// add data to packetif (mysql_num_rows($result) > 0) { $mark = mysql_fetch_row($result); }mysql_close($connection);// print packetecho wddx_serialize_value($mark);?>
Again, the array of five values is encoded as a WDDX packet, and sent to the requesting client, which decodes it and prints the data, together with an average of all five values.

<? // client.php - connect to server, retrieve and decode WDDX packet// url of Web page$url = "http://medusa/server.php?symbol=$symbol";// read WDDX packet into string$package = join ('', file($url));// deserialize$rates = wddx_deserialize($package);?><html><head><basefont face="Verdana"></head><body><h3>Five-day currency trends for currency symbol <? echo $symbol; ?></h3><table border="1" cellspacing="3" cellpadding="3"><tr><td>Data pointer 1</td><td>Data pointer 2</td><td>Data pointer 3</td><td>Data pointer 4</td><td>Data pointer 5</td><td>Average</td></tr><tr><?$sum = 0;// print rate datafor($x=0; $x<5; $x++){ $sum = $sum + $rates[$x]; echo "<td>$rates[$x]</td>";}// calculate average$avg = $sum/5;echo "<td>$avg</td>";?></tr></table></body></html>
Simple, huh?

 
 
>>> More PHP Articles          >>> More By Vikram Vaswani, (c) Melonfire
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: