HomePHP 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).
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:
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.