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

The Truth Is Out There - 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
Now that you've understood the fundamentals, let's look at a simple application of WDDX.

One of the most popular uses of this type of technology involves using it to get and display syndicated content from a content provider. Since WDDX is designed expressly for transferring data in a standard format, it excels at this type of task - in fact, the entire process can be accomplished via two very simple scripts, one running on the news server and the other running on the client.

Let's look at the server component first. We'll begin with the assumption that the content to be distributed (news headlines, in this case) are stored in a single database table, which is updated on a regular basis from an external data source. Every headline has a timestamp, which helps to identify the most recent. Consequently, we can postulate a table which looks something like this:

mysql> select * from news; +-------------------------------------------------------+------------------- --+ | slug | timestamp | +-------------------------------------------------------+------------------- --+ | Alien life found on Mars | 2001-08-09 10:46:21 | | Stem-cell controversy stirs up fresh protests | 2001-08-06 06:27:12 | | "Planet Of The Apes" opens nationwide | 2001-08-10 13:41:18 | | Schumacher one shy of all-time F1 record | 2001-08-04 18:23:33 | | Lucas reveals title of upcoming Star Wars installment | 2001-08-07 10:25:30 | | Computing power increases threefold | 2001-08-07 15:31:18 | +-------------------------------------------------------+------------------- --+ 6 rows in set (0.00 sec)
Now, we need to write a script which will reside on the server, connect to this table, retrieve the four most recent headlines, and encode them as a WDDX packet

<? // server.php - output WDDX packet containing four news headlines// database parameters$hostname = "content_server";$user = "wddx_agent";$pass = "823h3459";$database = "db6483";// open connection to database$connection = mysql_connect($hostname, $user, $pass) or die ("Unable toconnect!");// get four newest headlines$query = "SELECT slug FROM news ORDER BY timestamp DESC LIMIT 0,4";$result = mysql_db_query($database, $query, $connection) or die ("Error inquery: $query. " . mysql_error());// add headlines to array $serverSlugArrayif (mysql_num_rows($result) > 0) { while($row = mysql_fetch_row($result)) { $serverSlugArray[] = $row[0]; } }mysql_close($connection);// create WDDX packetecho wddx_serialize_value($serverSlugArray);?>
This is an extremely simple script - it performs a query to get the four most recent headlines, adds them to an array named $serverSlugArray, and then serializes and prints this array as a WDDX packet.

The requesting client now needs only to send a HTTP request to this script, read and deserialize the generated WDDX packet, and use it in whatever manner it sees fit. For this example, I'm using the data to create a scrollable tickertape containing the news items.

<? // client.php - connect to server, retrieve and decode WDDX packet// url of Web page$url = "http://content_server/server.php";// read WDDX packet into string$package = join ('', file($url));// deserialize$clientSlugArray = wddx_deserialize($package);?><html><head><basefont face="Verdana"></head><body><!-- this works only in IE --><marquee bgcolor="Black" loop="INFINITE"><font size=-1 color=white><b><?for($x=0; $x<sizeof($clientSlugArray); $x++){ echo $clientSlugArray[$x] . "       ";}?></b></font></marquee></body></html>
The first three lines are the most important - they take care of reading the URL into a single string (using standard file functions) and deserializing the WDDX packet into a native PHP array called $clientSlugArray. The remainder of the script simply iterates through this array and prints the elements (news headlines) within a <marquee> tag.

As you can see, using WDDX as the transport mechanism between servers offers a number of advantages - it's simple, flexible and far more efficient than coding your own solution to the problem. In fact, for certain business applications - content syndication is a prime example - it can save you a great deal of development and testing time.

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