PHP
  Home arrow PHP arrow Page 7 - Data Exchange with XML, WDDX and PHP
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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: starstarstarstarstar / 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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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 to connect!"); // 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 in query: $query. " . mysql_error()); // add headlines to array $serverSlugArray if (mysql_num_rows($result) > 0) { while($row = mysql_fetch_row($result)) { $serverSlugArray[] = $row[0]; } } mysql_close($connection); // create WDDX packet echo 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

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    Stay green...Green IT