Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 531
  • Last Modified:

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.

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:
<?php
//XMLXSL Transformation class
require_once('includes/MM_XSLTransform/MM_XSLTransform.class.php');
?><!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>RCI XML TEST</title>
</head>
<body>
This is an RCI XML Transformation test.
<br />
<br />
<form method="post" action="http://webtest1.rci.com/variableduration/gateway">
<table>
<tr>
<td align="left">
<input type="hidden" name="serviceRequest" value="<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>">
<?php
$mm_xsl = new MM_XSLTransform();
$mm_xsl->setXML("http://webtest1.rci.com/variableduration/gateway");
$mm_xsl->setXSL("rcitest.xsl");
echo $mm_xsl->Transform();
?></td></tr>
<tr><td align="right"><input type="submit" value="submit"></td></tr>
</table>
</form>
</center>
</body>
</html>

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
0
cacookejr
Asked:
cacookejr
  • 12
  • 9
1 Solution
 
nitinsawhneyCommented:
Hi,

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.

Cheers
0
 
cacookejrAuthor Commented:
You have my previous code, could u give me an example on using curl to finish off the rest of this call?
0
 
glcumminsCommented:
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):

<?php

$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,
            "serviceRequest=$requestContents");

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?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
cacookejrAuthor Commented:
glcummins:

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.
http://www.experts-exchange.com/Web_Development/Software/Macromedia_Dreamweaver/Q_22865193.html

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.
http://www.travelht.com/rci-curltest1.php

<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>RCI-Curl Test1</title>
</head>

<body>
THIS IS RCI-CURL TEST 1
<br />
<br />
<?php
/**      * 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_POSTFIELDS, XML_PAYLOAD);
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);
 }
 else
 {        $returnCode = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE);
 switch($returnCode)
 {
 case 200:
 break;
 default: $result = 'HTTP ERROR -> ' . $returnCode;
 break;
 }
 }


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

?>

</body>
</html>
Thank you for Responding and I hope to here back.
0
 
glcumminsCommented:
>> 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.
0
 
cacookejrAuthor Commented:
glcummins:

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.

Thanks
0
 
cacookejrAuthor Commented:
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


0
 
glcumminsCommented:
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.
0
 
glcumminsCommented:
Sorry, my previous post was in response to your second-to last post: 20004087
0
 
glcumminsCommented:
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.
0
 
cacookejrAuthor Commented:
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?
Thanks




0
 
glcumminsCommented:
Okay, I must have been using the original test (http://www.travelht.com/rcitest.php) instead of the cUrl test (http://www.travelht.com/rci-curltest1.php).

I ran it again using the correct page, and see that XML data is actually being returned (you have to View Source on the page that is returned, so it doesn't just look like a bunch of text.

So now (correct me if I am wrong), the only issue is parsing the returned XML document. For me to help you with that, I will need to know how you intend to use the data. Is it going to be inserted into a database, displayed in a specific format, etc?

SimpleXML (http://www.php.net/simplexml) is a fabulous PHP extension that makes it easy to access data from an XML document. However, it is available only for PHP 5. If there is a chance that you can upgrade, and you will be using XML documents a lot, it is worth the effort.

As an example, to access the 'completionCode' tag in the XML document that is returned by rci.com in your test example, you would simply use:

 echo $xml->responseEnvelope[0]->responseHeader[0]->statusData[0]->completionCode;

Notice how you simply follow the hierarchial structure to access the desired element. There are functions for returning all elements, elements matching specific patterns, elements of a certain depth (ie. three levels below the parent), etc.
0
 
cacookejrAuthor Commented:
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.

0
 
cacookejrAuthor Commented:
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
0
 
glcumminsCommented:
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.
0
 
cacookejrAuthor Commented:
I'm searching for that now - do u have a suggestion?
0
 
glcumminsCommented:
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.
0
 
cacookejrAuthor Commented:
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?
0
 
cacookejrAuthor Commented:
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.
0
 
glcumminsCommented:
>> 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.
0
 
cacookejrAuthor Commented:
gotcha
0
 
cacookejrAuthor Commented:
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
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 12
  • 9
Tackle projects and never again get stuck behind a technical roadblock.
Join Now