Forum Moderators: open

Message Too Old, No Replies

how to include xml results from an external server

         

rustyarmour

9:08 pm on Aug 10, 2010 (gmt 0)

10+ Year Member



Hi Guys,

I am working on a page that requires a form to be submitted to another server. The other server performs the calculations needed based on several input values from the user and sends back an xml response. So far, my javascript for submitting the form is working perfectly and the calculations are being performed without a problem.

My question is this:

How do I get the results for these four calculated results to load into my page instead of loading into an xml page in a new window?

here is the sample xml
<?xml version="1.0" encoding="UTF-8" ?>
- <Calculation>
<error />
- <result>
- <pressure>
- <yearly-yield unit="this">
- <![CDATA[ 14253.8
]]>
</yearly-yield>
- <per-unit="this">
- <![CDATA[ 52803.2
]]>
</per-unit>
- <yearly-saving unit="this">
- <![CDATA[ 15524.8
]]>
</yearly-saving>
- <yearly-other-saving unit="this">
- <![CDATA[ 4176.3
]]>
</yearly-other-saving>
</pressure>
</result>
</Calculation>

Any assistance in helping me wrap my head around this would be appreciated.

daveVk

2:39 am on Aug 11, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You need AJAX to do that, using one of the libraries will make that a lot simpler.

I have combined two examples from [api.jquery.com...] below as a guide

$.post("test.php", $("#testform").serialize(),
function(data){
process(data);
}, "xml");

This replaces your submit code.

rustyarmour

2:07 pm on Aug 11, 2010 (gmt 0)

10+ Year Member



Hi DaveVk,

I see this note included in the text on the page. This is exactly what I am trying to due. The information IS being processed on a differnet domain.

Due to browser security restrictions, most "Ajax" requests are subject to the same origin policy; the request can not successfully retrieve data from a different domain, subdomain, or protocol.

daveVk

12:45 am on Aug 12, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Search "jquery cross domain ajax" for a workaround that may suit your case. I assume you can not change the response from the other domain (json solution), so a server side solution such as a PHP proxy may be the go.

rustyarmour

9:32 pm on Aug 12, 2010 (gmt 0)

10+ Year Member



HI DaveVk,

$.post("test.php", $("#testform").serialize(),
function(data){
process(data);
}, "xml");

The above code you gave me DOES give me the results from the server. I get an xml page with the data results based on the data I submitted. So, back to the same question: How do I get the resulting xml to load into my page instead of loading into another xml browser window?

Sorry if I am being a little on the dense side.

daveVk

12:07 am on Aug 13, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The AJAX request alone should not do that. Make sure you are not submitting the form as well as making the ajax request. If using an onsubmit function make sure it always returns false to stop submit.

During testing replace process(data); with alert(data);

Did you find a workaround to "same origin policy" ?

rustyarmour

12:26 am on Aug 13, 2010 (gmt 0)

10+ Year Member



Not yet...this thing is driving me nuts

astupidname

6:35 am on Aug 13, 2010 (gmt 0)

10+ Year Member



I would not necessarily use ajax for this (don't need no stinkin jquery, or YQL), it's simple enough from the server side (your server, as opposed to Yahoo's [YQL used for jquery cross-domain ajax plugin] or whatever). Also I would not actually use the remote site's url for the form's action, rather use a php page on your server to post the form data ("behind the scene" so to speak) to the other site's url and retrieve the response and then parse the xml response how you like. All via php and the oft forgotten 'context' parameter of some filesystem functions such as fopen(), or file() or file_get_contents(). For example, say on your page you have this form:

<form action="response.php" name="someForm" method="post">
Name: <input type="text" value="" name="myFormsNameField"><br>
email: <input type="text" value="" name="myFormsEmailField"><br>
<!-- etcetera, more fields and stuff -->
<input type="submit" value="submit">
</form>


Then the response.php file which that form's action is set to can retrieve the form fields and pass them along to the remote site via POST (could use GET as well, but I'm assuming the need for POST), utilizing the context parameter of fopen with a context resource created by stream_context_create() [us3.php.net...] (note php 5.0+ required for support of context's with the filesystem functions)(could probably use file_get_contents as well, but for this example I'll show fopen):

<?php
//>response.php

/***
* you will of course do verification of the $_POST data here first for validity and presence,
* then we will prepare to POST it to the other site.
* $postArr should be an array with keys named as the receiving site expects the POST fields to be,
* the values can be from whatever named field on the form you want, or anywheres really.
***/
$postArr = array(
//remote site expects a 'name' POST parameter, will use our forms 'myFormsNameField' value
'name' => $_POST['myFormsNameField'],
'email' => $_POST['myFormsEmailField']
);
/***
* or you could possibly do instead:
* $postArr = $_POST;
* if you are o.k. with sending the remote site everything from the $_POST array
* and your form's fields are all named exactly as the remote site expects the POST fields to be.
***/
/********** ATTENTION! replace example url below ($postToFileName) with url of the remote site you want to POST to**********/
$postToFileName = 'http://www.example.com/return.php';
$opts = array(
'http' => array(
'method' => 'POST',
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'content' => http_build_query($postArr)
)
);
$context = stream_context_create($opts); //creates a streams context resource
$fp = @fopen($postToFileName, 'r', false, $context);
$returnedMessage = '';
if ($fp !== false) {
while (!feof($fp)) {
$returnedMessage .= fgets($fp);
}
fclose($fp);
/***
* o.k. now $returnedMessage contains the contents of the remote
* site's response to our POST of the form fields to them.
* Time to parse the xml response from the remote site,
* first converting our string ($returnedMessage) into an xml object.
* Please note, I am assuming the node in the xml which you show as
* being named 'per-unit' is actually named just 'per' ?
* Also, this is just based upon one result in the returned xml
* as shown by you, is perhaps a bit simplistic without error checking,
* but is after all mainly example purposes here!
* Though might work right off the bat... the 'per-unit' thing being a possible glitch
***/
try {
$xml = new DOMDocument();
$xml->preserveWhiteSpace = false;
$xml->loadXML($returnedMessage);
$result = $xml->getElementsByTagName('result')->item(0);
$pressure = $result->getElementsByTagName('pressure')->item(0);
$yearlyYield = $pressure->getElementsByTagName('yearly-yield')->item(0)->nodeValue;
$per = $pressure->getElementsByTagName('per')->item(0)->nodeValue;
$yearlySaving = $pressure->getElementsByTagName('yearly-saving')->item(0)->nodeValue;
$yearlyOtherSaving = $pressure->getElementsByTagName('yearly-other-saving')->item(0)->nodeValue;

echo "Yearly Yield: $yearlyYield<br>Per: $per<br>Yearly Saving: $yearlySaving<br>Yearly Other Saving: $yearlyOtherSaving";
} catch($er) {
echo 'UNABLE TO PARSE RESPONSE';
}
} else {
echo '<h3 style="color:red;">Unable to open file, no response available!</h3>';
}

?>


Of course you could access the "response.php" file via ajax and post with the form's fields serialized or whatever, but the ajax is totally unnecessary/not used in this example.

rustyarmour

11:46 am on Aug 13, 2010 (gmt 0)

10+ Year Member



heavy sigh...form works when submitted directly to the remote server (xml comes back fine). Of course, they've changed my form parameters, but I've made what I think are the proper corrections to the response.php page. When I submit the form to the response.php page, I am getting a 500.

I'll send you sticky mail. If you wouldn't mind taking a look at the info, I'd really appreciate it. I'd like to know where I am going wrong.

astupidname

5:39 pm on Aug 13, 2010 (gmt 0)

10+ Year Member



A few small errors in your code, one in mine:
} catch($er) {

Should have been (silly php wants to be told what to catch):
} catch(Exception $er) {


Hopefully ironed out for the most part now!

rustyarmour

7:32 pm on Aug 13, 2010 (gmt 0)

10+ Year Member



Hey astupidname, I really appreciate your help on this. I tried reloading everything, but I still get a 500 with IE, and a blank page load with firefox. Any ideas? I am about reaady to pull what's left of my hair out....believe me it ain't much, but I sure would miss it :)

astupidname

6:42 am on Aug 14, 2010 (gmt 0)

10+ Year Member



It's working perfectly on my end with the fixes I sent you. I guess I'm making the assumption that you have php available on the server, correct? Only thing I can think of otherwise is perhaps you are running old version of php such as 4 (just as naughty as using IE6 IMO :)? Try this test page:

<?php
echo PHP_VERSION;
?>


Does that spit out anything? If so, what?

rustyarmour

4:11 pm on Aug 14, 2010 (gmt 0)

10+ Year Member



Yep...running 5.2.5

I am going to take another look at everything. Perhaps a fresh pair of eyes will do the trick.

rustyarmour

4:04 am on Aug 15, 2010 (gmt 0)

10+ Year Member



OK,

One more time! I could really use all the help I can get here. This thing was supposed to be live yesterday...best laid plans of mice and men! :^(

OK, I copied the files over to another host. At least this time I got a response - Unable to open file, no response available! (which is the else echo) So at least I know something is "not happening" on the original server that should be because I couldn't even get that.