Forum Moderators: open
<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>
<?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>';
}
?>
<?php
echo PHP_VERSION;
?>