PHP
  Home arrow PHP arrow Page 7 - Data Exchange with XML, WDDX and PHP
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Data Exchange with XML, WDDX and PHP
By: Vikram Vaswani, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 4
    2001-09-13

    Table of Contents:
  • Data Exchange with XML, WDDX and PHP
  • The Wonderful World Of WDDX
  • Polly Wants A Cracker
  • Humbert Redfinch-Northbottom The Third, I Presume?
  • Old Friends And New
  • Hip To Be Square
  • The Truth Is Out There
  • Money Talks
  • Closing Time

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Data Exchange with XML, WDDX and PHP - The Truth Is Out There


    (Page 7 of 9 )

    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


     

       

    PHP ARTICLES

    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
    Stay green...Green IT