| |
|
 |
Providing Web Services using WDDX
|
As we've seen in lot of articles and publications, the web of services, is one of the emerging evolutions of the actual document-based web. To make the web a valid media for providing services it would be needed to increase the inter-operability between existing web applications and design new ones with this in mind.
WDDX is a standard developed to interchange data structures between different programming languages using XML as the payload. From their authors WDDX is:
"... is a mechanism for exchanging complex data structures between application environments. It has been designed with web applications in mind. WDDX consists of a language and platform neutral representation of instantiated data based on XML 1.0 (which is defined using this DTD) and a set of serializer/ de-serializer components for every environment that uses WDDX. The process of creating an XML representation of application data is called serialization. The process of instantiating application data from a WDDX XML representation is called de-serialization. "
Let's see how this could be useful in several circumstances, for example an organization might have a huge collection of documents about a given topic and they may want to offer as a search service for the collection using HTTP. They can easily create a script in PHP,perl or whatever that receives parameters as GET/POST data and search the collection for the given words. The problem is that they can't do anything useful with the result. The result output should be a neutral output that can be easily handled by organizations or individuals wanting to use/rent/buy the service. This one of the key application fields for WDDX
Why not just XML?
Our model company may think to provide the result of their search service in some XML vocabulary, this is neutral and could be a valid solution, the advantage of WDDX over this solution is that WDDX is XML(!) and that WDDX content can be managed from a programming language without the need to parse or convert the XML data. In my opinion XML vocabularies are great for data interchange but we have to think in WDDX or XML-RPC for interchanging services.
Enter PHP
PHP has a native extension for WDDX, we should have PHP compiled to use this extension by adding --enable-wddx to the configuration script
WDDX has two basic operations: serializing data from PHP data types to WDDX and de-serializing data from WDDX to P{HP data types, we are going to cover both.
Serialization
PHP has different ways of serializing PHP variables into WDDX packets, when we want to serialize an isolated variable we can use the following syntax:
string wddx_serialize_value(var,[comment]); |
Example:
$wddx=wddx_serialize_value($foo); |
What we do in this function is converting the $foo variable (whatever it is) to a WDDX packet ready to be transmitted and used. $foo can be a complex structure, an associative array of arrays of asoc arrays etc... The result of the wddx_serialize_value function is a string containing the wddx packet
WDDX's anatomy
We don't want to cover the WDDX vocabulary itself in this article since PHP does all the work for us, but if you are curious here's an example of a WDDX packet:
<wddxPacket version='1.0'>
<header/>
<data>
<string>hello</string>
</data>
</wddxPacket> | If we want to serialize more than one variable we have to create a wddx packet first, then add variables one by one to the packet and after all close the packet
wddx_add_vars($paq,"foo");
wddx_add_vars($par,"name");
$wddx=wddx_packet_end($paq); |
In the code above we create a packet with wddx_packet_start and then we add $foo and $name to the packet, finally wddx_packet_end closes the packet and returns a string containing the WDDX packet.
Please note that the wddx_add_vars function receives the name of variables to be serialized and not the variables. wddx_serialize_value and wddx_add_vars receive different type of parameters, remember that.
De-serialization
To de-serialize a wddx packet we use:
$result=wddx_deserialize($wddx); |
The function receives a wddx packet and returns the de serialized variable or, if the structure is composite, an associative array containing var=>value pairs. Once de-serialized you just use the data as PHP variables doing whatever you want with them.
Packets serialized with WDDX can be de-serialized in other languages as Perl, Python, C, C++ etc. This allows interoperatibility between services programmed in different languages provided they use WDDX as the data interchange mechanism. WDDX is then a good option to provide web services, you write the service in your favorite language and output a WDDX packet, machines wanting to use the service just read the wddx packet, deserialize it and use it in any other language.
We've successfully used wddx to provide cross-search services between sites (ie: you can search other's sites databases from out interface and you can use our search engine from other sites too and there are lot of areas where wddx can be used.
Luis Argerich
Comment List
| Topic: |
Author: |
Time: |
|
how to serialize a db query into WDDX
|
chris k
|
11.09.2001 02:53
|
|
while the tutorial did show how to
serialize a variable into an WDDX as well as oulining the use/idea behind WDDX i was wondering if anyone could maybe tell a rookie like me how i would actually be able to serialize a db query into a WDDX packet? picture the following:
//
//
mysql_select_db($dbName);
$query = "select * from tableName";
$result = mysql_query($query);
//
// now i dont know how to proceed =(
//
|
|
RE: how to serialize a db query into WDDX
|
Young Jin Lee
|
19.10.2001 00:40
|
|
check the following article.
http://www.phpbuilder.com/columns/jesus20000402.php3
Basically you are enterring all the resulted rows into the rowvariable[] and serializing with Wddx function.
cheers
|
|
 |
|
|