We help IT Professionals succeed at work.

I need some guidance and examples of doing a form post to an external server, recieving the XML data and parsing/transformming it for display.

cacookejr asked
Last Modified: 2008-09-22
I need some guidance and examples of doing a form post to an external server, recieving the XML data and parsing/transformming it for display.

This is a linux server running php 4.4.4 with pear/Curl/Soap XMLDOM XSLTDOM etc already compiled in.

This test is a very simple form that posts the data to the merchant server as a hidden field.
I don't know how to get the response back in a form that can be displayed client/browser side.
I have tried to use the built in XML transformation tools in dreamweaver 8 but can't get that figured out either.
Sample code:
//XMLXSL Transformation class
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>RCI XML TEST</title>
This is an RCI XML Transformation test.
<br />
<br />
<form method="post" action="http://webtest1.rci.com/variableduration/gateway">
<td align="left">
<input type="hidden" name="serviceRequest" value="<requestEnvelope>
$mm_xsl = new MM_XSLTransform();
echo $mm_xsl->Transform();
<tr><td align="right"><input type="submit" value="submit"></td></tr>

When the form is submitted it actually takes you to a new page on the merchant server and just displays the raw XML.

I need a way to receive the xml data back format it and display in my own page.

I hope that makes sense.

Like most things, this is urgent
Watch Question


The basic idea is when the form is submitted, you submit it to a script on your server that script will build the form params and send a request to the merchant server using curl, and then will act upon the response accordingly.



You have my previous code, could u give me an example on using curl to finish off the rest of this call?
Let's start with a simple cUrl post example. This is taken from the cUrl website (http://curl.haxx.se/libcurl/php/examples/simplepost.html):


$requestContents = null;

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,"http://webtest1.rci.com/variableduration/gateway");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,

curl_exec ($ch);
curl_close ($ch);

This example simply posts the data to the remote server, but does not expect anything in return. In fact, if you run it, the results will be displayed in the browser as-is, with no chance for you to format or process it.

Since you need to be able to process the data that is returned, we will need to add two things. One, we will need to tell curl to expect a response, and create a variable to hold that response. Two, we will need to populate $requestContents with the value that you want to send in the field 'serviceRequest'. Since you have already done that in your code above, I leave the second part up to you.

To get cUrl to store the results from the remote server in a variable, we need to add this line:

 curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);

Then, instead of:

 curl_exec ($ch);

we will use:

 $returnValue = curl_exec($ch);

With the curl_setopt() statement, we told cUrl that we should expect data to be returned, and then we simply store that data in the $returnValue variable. Now you will have the data in a variable where you can process it and display it as needed.

Does this get you closer to your goal?



I appreciate your answer and that is pretty much what I have already done - just got it working about a half hour ago.  I was hoping there was some way to use dreamweaver 8 to help with this and the parsing (XSLT) and I have another ticket already open for that.

I finally figured out that I would have to drop back to Curl for now to be able to commincate with this Vendor.

I now have a problem which is related and maybe you can help with that also.

When I run my code now it works correctly BUT I need the info sent back to me in XML and some way to parse it.

Right now I'm getting it back as just straight data.
Am I incorrect in his?
Is there a way that I can get the XML back and parse it BEFORE displaying it?

Here is my current code and the URL so you can see the results.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>RCI-Curl Test1</title>

<br />
<br />
/**      * Define POST URL and also payload     */
define('XML_PAYLOAD', '<?xml version="1.0"?><requestEnvelope><requestHeader><controlData><clientName>XMLBusinessPartnerChannel</clientName><userID>xmlweeksonly</userID><password>xmlweeksonly</password></controlData><processData><operationName>quickSearch</operationName></processData></requestHeader><SearchRequest><startDate>11/01/2007</startDate><endDate>01/01/2008</endDate><lengthOfStay>7</lengthOfStay><minUnitSize>H</minUnitSize><regionCode>FLOR</regionCode></SearchRequest></requestEnvelope>');
define('XML_POST_URL', 'http://webtest1.rci.com/variableduration/gateway');
/**     * Initialize handle and set options     */
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, XML_POST_URL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 25);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: close'));
/**     * Execute the request and also time the transaction     */
$start = array_sum(explode(' ', microtime()));
$result = curl_exec($ch);
$stop = array_sum(explode(' ', microtime()));
$totalTime = $stop - $start;
/**     * Check for errors     */

if ( curl_errno($ch) )
 {        $result = 'cURL ERROR -> ' . curl_errno($ch) . ': ' . curl_error($ch);
 {        $returnCode = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE);
 case 200:
 default: $result = 'HTTP ERROR -> ' . $returnCode;

/**     * Close the handle     */
/**     * Output the results and time     */
echo 'Total time for request: ' . $totalTime . "\n";
echo '<br>';
echo '<br>';
echo $result;
/**     * Exit the script     */


Thank you for Responding and I hope to here back.
>> I need the info sent back to me in XML

This is up to the provider on the other end. You will not have control from your end regarding the format of the data that is returned by the remote server. If the provider provides some sort of API that you can manipulate to return the data in XML, then you will need to modify your code to turn on that switch or flag or whatever is needed. Otherwise, you will have to take what they give you.

>> and some way to parse it.
Right now your code simply displays the contents of $result:

 echo $result;

Instead of echoing the contents of that variable, run your parsing routine against that variable first, and output as needed.



Actually XML Transformation (XSLT) may be a better term than parsing - don't really know which.
If u look back up to my first post in this ticket u will see that I should have everything I need installed to be able to do this.  I also have the XML schema's for all the responses - I just need some hand holding.
If u can work this out it may close both tickets for you.



to ur response, in the first test the result was actually raw XML but it was in a NEW page deleivered by their server - that was my problem.  I kew of no way to grab it.

U can still see this test at http://www.travelht.com/rcitest.php - just hit SUBMIT

Thanks - u r helping alot

Right, but the first caveat still applies: If the remote provider is not serving XML, there is no way for you to receive XML from them, regardless of transforms.

From the W3C documentation for XSLT (http://www.w3.org/TR/xslt):

"This specification defines the syntax and semantics of XSLT, which is a language for transforming XML documents into other XML documents."

If the provider is serving HTML (not XHTML) or something else, there will be no way to make it XML without a lot of manually-created parsing code.

Please forgive me if I am missing something obvious here.
Sorry, my previous post was in response to your second-to last post: 20004087
Okay, I've run the test via the site you provided, and see that it is actually XML being returned. I'm concerned that the browser is actually being redirected to the rci.com site. This should not happen if the cUrl transfer is being performed successfully. This is the behavior that should be expected if a redirect via header() was being performed somewhere. Even in the event that the cUrl response was just being dumped to the browser, the address bar would still indicate your site (travelht.com) rather than the remote site.


I belive that the data is actually being sent back to my server - not being redirected to the rci server - but u r the expert.

I read somwhere on the web today that it was possible to tell curl in what format u wanted the data sent back and it defaults to a string instead of the XML, but there may be a flag that can be set to get the XML instead of the string?

A form post sends me the data in XML opening a new page on the RCI server which I can't do anything with.
Curl currently is sending me a data string back to MY server.

they provide no support for MY side - they only support their own servers.
So they r of no help.

I don't understand how to parse that string when I have no way know how to seperate the data.

Maybe I could do a tranformation on the data if I got it back in XML and could transform that BEFORE it is display.

Am I making any sense?

Unlock this solution and get a sample of our free trial.
(No credit card required)


HAH, if u look at the source of that results page the data actually is in an XML format. I just now need to figure out how to transform it.


would this be of any use?

<?xml version=1.0?>
<!ELEMENT requestEnvelope      (requestHeader, SearchRequest)>

<!ELEMENT requestHeader      (controlData, processData)>
<!ELEMENT controlData      (clientName, userID, password)>
<!ELEMENT clientName      (#PCDATA)>
<!ELEMENT userID      (#PCDATA)>
<!ELEMENT password      (#PCDATA)>
<!ELEMENT processData      (operationName)>
<!ELEMENT operationName      (#PCDATA)>

<!ELEMENT SearchRequest      (startDate, endDate, lengthOfStay, minUnitSize?, regionCode)>
<!ELEMENT startDate      (#PCDATA)>
<!ELEMENT endDate      (#PCDATA)>
<!ELEMENT lengthOfStay      (#PCDATA)>
<!ELEMENT minUnitSize      (#PCDATA)>
<!ELEMENT regionCode      (#PCDATA)>

Please reply
That provide the definition of the data that is being returned. You will still need to indicate exactly what you plan to do with the data before a definitive answer can be given.


I'm searching for that now - do u have a suggestion?
No, that part is entirely up to you. It depends on how you intend to use the data. If it is data that will be used repeatedly, you probably need to store it in a database or file. If it only needs to be used once, you will need to decide how you want it to appear on your page.

I don't know what you need for the final result, so I cannot provide specific guidance in that area. Once you know what you intend to do with the data, I will be able to provide details regarding the implementation.


right now I would like to just display that data in a table and remove un-need fields and then I should be on my way - YEEHAH

IF u can help with that it would be fantanstic

Second Question: Is it proper to award more than 500 points for this issue?


The data is a feed that will change depending on the request the client-side sends - so it needs to stay dynamic - it is a feed from a vendor - I can work out sending the XML request with no problem now.
>> display that data in a table and remove un-need fields
Can you provide an example of what data will or will not be included in the table?

>> more than 500 points
No, but cash donations are always accepted. ;)

I am now offline for the night, but will return in the morning for more.




when u see this: I can't seem to match up the DTD or the request with the returned data.  U have the same info that I have now.  Maybe it makes sense to u.

Time to crash and thanks again

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.