Products Drag & Drop Objects Drag & Drop Calendar Drag & Drop Form Code View
Order
Testimonials Downloads
dynamicreport.com News Members Forum Knowledge Base Site Map
Support Support Ticket Contact Us

Knowledge Base Print printer-friendly page.

Login
ddobj
Forum
News


Article #1019: PHP Client Server XML Communication

 

XML is the standard form of communication (parameter passing) to disparate systems. Especially those systems that do not share databases. XML is the perfect solution for transmitting information to other systems that process and store information in the form of records in a data repository.

This article seeks to provide the reader with a complete yet concise implementation of both client and server XML processors using PHP v5+. Part 1 explains the client implementation as part of the overall sample application, and should provide enough reader insight for further customization. Part 2 explains the server implementation as part of the overall sample application, again, with the idea that developer modifications will fulfill the reader's own application requirements. Each of the two parts are followed by a snippet of the client and server implementation, respectively. The compressed archive is available for download, which contains files and folders including client (request) and server (result) XML templates.

Part 1: Client XML Request
The client portion of this application should be placed on the Web server where the calling or sending system is situated. When client.php is invoked it will load the XML request template file, which contains the parameters or fields without values. These parameters will be assigned values by the client.php script based on your application's requirements. It may obtain values from a database so that it is dynamic or they may be constants. Either way it is convenient to store the XML fields in a file rather than being inline with the script.

The client then proceeds to set the date/time of the XML timestamp attribute. This is implemented here for example purposes incase the receiving system would like the date and time of the XML transfer.

A few other XML parameters are assigned such as the login that will be verified later by the server XML processer script (server.php), user detailed information, notes and order information. The order field was included here to demonstrate that an order entry system could submit orders to another system (server) in the form of XML.

Once parameters have been assigned it is time to convert the structured XML object into plain text output; stored as variable $request.

Next we setup the header that details the raw information we are sending to the server - in this case content type: text/xml. The XML output in $result follows the standard header information.

It is now time to initialize the means by which we transmit the XML header (server-understood format) directly to the receiving end (server.php). Since we are using Web based technology we'll transmit the XML data over by the HTTP POST protocol using the Client URL library. But first we must specify some crucial CURL parameters that regulate the client-server transmission. The exact URL address to the server script (server.php) must be specified. The transmission timeout value is optional, and it has been set to 30 seconds. CURLOPT_POST specifies the HTTP POST protocol, and the CURLOPT_POSTFIELDS sends the XML data. CURLOPT_RETURNTRANSFER returns the output of the server so that it can be processed by the client. CURLOPT_HEADER sends the entire XML header and data. The server output is assigned to variable $result following transmission. It is good practice to close the client url resource upon completion.

The client XML request is written to a log file for record keeping. The server result is also written to a log file. It could be processed as an XML object and written to a database instead, but for simplicity we wrote the returned XML data to an XML file.

Lastly, we output a message from the client based on the server's XML result to indicate the result on-screen.

It is time to look at how the server processed the above client's XML header transmission.

View file: client/client.php for details.

<? /********* CLIENT */

// load XML client request template
$xml = simplexml_load_file ("templates/request.xml");
// set some XML attributes (mark the request with the date/time)
$xml->attributes ()->timestamp = date ("Y-m-d H:i:s");
// obtain and set all information necessary for order data transfer
// may need htmlspecialchars(utf8_encode($var)) for text with &, <, >
// if encoding requires it and if it's not automated by simplexml version
$xml->Login->User = "jdoe";
$xml->Login->Password = "pw";
$xml->Login->Email = "jdoe@my-domain.com";
// assign XML fields with values
$xml->Details->First = "John";
$xml->Details->Last = "Doe";
$xml->Notes->Description = "Example only.";
// assign XML fields with comma delimited values
$xml->Order->ItemID = "1121,1140,3009,1343";
$xml->Order->Quantity = "5,3,11,21";

$request = $xml->asXML (); // convert to well-formed XML output

// create header
$header[0] = "Host: www.my-domain.com";
$header[1] = "Content-type: text/xml";
$header[2] = "Content-length: ".strlen ($request)."\r\n";
$header[3] = $request;

// send using curl
$ch = curl_init (); // modify for correct server processor URL
curl_setopt ($ch, CURLOPT_URL, "http://www.my-domain.com/xml/server/server.php");
curl_setopt ($ch, CURLOPT_TIMEOUT, 30);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $request);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); // return server output
curl_setopt ($ch, CURLOPT_HTTPHEADER, $header); // send header
$result = curl_exec ($ch); // returned XML server result
curl_close ($ch);

// store log of XML client request
$fp = fopen ("logs/request.xml", "w");
fwrite ($fp, $request, strlen ($request));
fclose ($fp);

// store log of XML server result
$fp = fopen ("logs/result.xml", "w");
fwrite ($fp, $result, strlen ($result));
fclose ($fp);

echo "<p align = \"center\">View file: <a href = \"logs/result.xml\"".
 " target = \"_blank\">logs/result.xml</a> for details.</p>";
$xml_result = simplexml_load_string ($result); // obtain XML server result
echo "<p align = \"center\">Server generated ID: ".
 $xml_result->ServerID."</p>";

?>

Code View - dynamicreport.com

Part 2: Server XML Result
When invoked the server begins by obtaining the XML header sent from the client (client.php). The $GLOBALS['HTTP_RAW_POST_DATA'] value yields the XML header posted to the server by the client. The XML output is transformed into a structured XML object that can be processed element by element. A log file of the client sent XML data is stored to an XML file for record keeping.

The next step for the server is to load the XML template as a basis for assigning and sending information back to the client. The client XML credential parameters are checked to verify the integrity of the transmission. The server XML parameters are assigned values based on the success or failure of the checks. The reader may decide to check credentials against a database for verification. An error is assigned to the <Error /> element when validations fails.

The server XML has been populated with data that will be stored into a log file on the server before it is output from the server. The output is sent back to the client upon completion of script execution.

The server output will be accessible to the client, since the client set the client URL (CURLOPT_RETURNTRANSFER) option.

View file: server/server.php for details.

Note that error checking was excluded for simplicity sake.

<? /********* SERVER */

// obtain client XML from sent header
$request = $GLOBALS["HTTP_RAW_POST_DATA"];
$xml = simplexml_load_string ($request);

// store log of client XML request
$fp = fopen ("logs/request.xml", "w");
fwrite ($fp, $request, strlen ($request));
fclose ($fp);

// load XML server result template
$xml_result = simplexml_load_file ("templates/result.xml");

// validate & initialize returned XML server result
if (strcmp ($xml->Login->User, "jdoe"))
 $xml_result->Error = "[Invalid UserID]"; // invalid user
if (strcmp ($xml->Login->Password, "pw")) // invalid password
 $xml_result->Error = $xml_result->Error."[Invalid Password]";
if (strlen ($xml_result->Error) <= 0)
 $xml_result->ServerID = "1000"; // generate ID if no error

$request = $xml_result->asXML (); // convert to well-formed XML output

// store log of XML server result
$fp = fopen ("logs/result.xml", "w");
fwrite ($fp, $request, strlen ($request));
fclose ($fp);

echo $request; // send XML server result output to client

?>

Code View - dynamicreport.com

 
 

Back to article listing

 


 

Copyright © 2007-2008 Interaxis. All rights reserved.
Contact Webmaster