ArrayToXML Readme

Overview

ArrayToXML is simple way to setup a XML gateway or web service without needing to know or messing around with XML, HTTP Requests or Web Services. All you need to know to use ArrayToXML is how to use arrays.

Just setup your ArrayToXML Client one machine and your ArrayToXML Server on the other machine, then create your array on one side and it will be available on the other. Build your own requests and responses in the form of arrays. Minimum knowledge is needed.

This class is released under the GPL License. You may use, distrobute and alter this script as you see fit. All I ask is that the copyright and author information be left as is.

[back to top]

Requirements

[back to top]

Installation

All you need to do to install ArrayToXML is to put the class file (ArrayToXML.class.php) in your http directory on each machine.

For information on calling the class see the next section - Instructions

[back to top]

Instructions

Client Side

Setting up client requests is easy. First include the class file:

require_once( "./ArrayToXML.class.php" );

Next create your request array (the array you want to send to the server side):

$myArray = array( "name"=>"Joe", "age"=>"36" );

Now Simply call the client class, feeding it your new array and the location of your server script:

$Request = new ArrayToXMLClient( "http://localhost/server.php", $myArray );

This sends off your request and receives an array back from the server. To get your response array simple call getResponseArray():

$responseArray = $Request->getResponseArray( );

Its as easy as that.

If an error occurs in your server script during the request you will receive an array similar to this:

array(2) {
["error"]=>
string(108) "An error occurred during the Request. Please ensure your server
url is correct and PHP is running with PEAR."
["dump"]=>
string(13) "HTTP Code 404"
}

If you run into problems with your response, I recommend using var_dump( $responseArray ) to dump the array to the screen. This allows you to see if you are getting your expected response or and error.

Server Side

As with the client side setting up the server side is easy. Again include the class file:

require_once( "./ArrayToXML.class.php" );

Now simply call the server class as follows:

$Server = new ArrayToXMLServer( $HTTP_RAW_POST_DATA );

This can be called as is. $HTTP_RAW_POST_DATA is a PHP variable which should be automatically set when your script runs. Next get the array which has been sent by the client side.

$myArray = $Server->getRequestArray();

This is an identical array to that which you sent on the client side. Now do something with this information:

$string = "Hello {$myArray['name']}! You don't look {$myArray['age']}";

$responseArray = array( "response" => $string );

Now that we have created a new array to send back, lets send it.

$Server->sendResponse($responseArray);

Important: Don't output anything on the server script (echo, print, etc.). Doing so will cause an error array response.

This example is included (simpleExample.php, simpleServer.php)

[back to top]

Examples

There are a couple of examples available with this package

Simple Example

This is a very basic example. It simply sends a name and age and the server returns a sentence containing the two. Check out the files (simpleExample.php, simpleServer.php) or Instructions above to see how this one works.

Commands Example

This is a basic example which sends commands to the server which the server then runs and sends back the response. Check out the files (commandExample.php, commandServer.php) to see how they work.

[back to top]