0

I am having some trouble getting an xml response from a server, this is the first time I have coded with xml in PHP using cURL (posting parsed xml to server, and receiving a response xml) - all the code is below:

HTML:

<html>
<head>
<title>test</title>
</head>
<body>
<form action="3FieldSample.php" method="post" enctype="multipart/form-data" >
<div class="form-field title">
    <div class="input select"><label for="title">Title</label>
    <select name="title" id="title" value="mr">
<option value="mr">Mr</option>
<option value="mrs">Mrs</option>
<option value="dr">Dr</option>
<option value="miss">Miss</option>
<option value="ms">Ms</option>
</select></div>     </div>


<div class="form-field float-left">
        <div class="input select"><label for="fname">First Name</label>
        <input name="fname" value="" maxlength="150" width="100%" type="text" id="fname" /></div>
</div>

<div class="form-field float-left">
            <div class="input text"><label for="ApplicationPaydayAppLastName">Last Name</label>
            <input name="lname" value="" maxlength="150" type="text" id="lname"/></div>     
        </div>
<div class="submit">
 <input type="image" src = "submit.jpg" width="25%" alt="submit" id="submit">
</div>
</form>
</body>
</html>

===============================================

The processing:

<?php
header('Content-Type: text/xml');
$xmldoc = new DomDocument( '1.0' );
$xmldoc->preserveWhiteSpace = false;
$xmldoc->formatOutput = true;
$url = "http://thesite_2_post_2.com/process.php";
$title = $_POST['title'];
$fname = $_POST['fname'];
$lname = $_POST['lname'];

$post_string = '<?xml version="1.0" encoding="UTF-8"?>     
<lead>
<applicant>
        <title>'. $title.'</title>
        <fname>'. $fname.'</fname>
        <lname>'. $lname.'</lname>
</applicant>
</lead>';

$header  = "POST HTTP/1.0 \r\n";
$header .= "Content-type: text/xml \r\n";
//#$header .= "Content-type: text/html \r\n";
$header .= "Content-length: ".strlen($post_string)." \r\n";
$header .= "Content-transfer-encoding: text \r\n";
#$header .= "Connection: close \r\n\r\n"; 
$header .= $post_string;
print ($post_string);

$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0); 
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $header);
###curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

$data = curl_exec($ch); 
curl_close($ch);
$objResult = json_decode($data);

#
#  This is an example of how I'm supposed to deal with the response?  
#  
#
$_SESSION['price'] = 0;
if ($objResult->purchased == 1) {
$_SESSION['price'] = $objResult->price;
$url = urlencode($objResult->redirect_url);
header('Location: '.$url);
print $objResult;
}
else
{
   echo "something went wrong";
}

?>

The last snippet above is an example of how I'm supposed to deal with the response which I'm not getting. This is the output I am getting : This page contains the following errors: error on line 8 at column 8: Extra content at the end of the document. Below is a rendering of the page up to the first error.

mr peter griffin

When I view the source I get this output (without the leading hashtags):

#<?xml version="1.0" encoding="UTF-8"?>     
#<lead>
#<applicant>
    #<title>mr</title>
    #<fname>peter</fname>
    #<lname>griffin</lname>
#</applicant>
#</lead>something went wrong

Any help in getting a response xml from the server would be appreciated.

1
  • 1
    And your question is? Is it the error message? What is unclear with it for you? Also please re-create an example from scratch that is demonstrating your issue with as little data and code as necessary. That most often helps you already to remove any errors. Commented Oct 11, 2014 at 15:33

1 Answer 1

1

cURL handles many details of the HTTP request for you, including headers. Sending the XML document could be simplified like this:

[...]
$cu = curl_init($url);
curl_setopt($cu, CURLOPT_POSTFIELDS, $post_fields); // This implies CURLOPT_POST = true.
curl_setopt($cu, CURLOPT_RETURNTRANSFER, true);
curl_setopt($cu, CURLOPT_HTTPHEADER, [
  "Content-Type: text/xml"
]);
$data = curl_exec($cu);
[...]

You are printing the XML document and later executing header function. This won't work, because headers must be set before any output is done.

As a side note, you should be cautious inserting POST parameters without validation in your XML. This code:

$title = $_POST['title'];

poses a vulnerability, because an attacker can send anything in HTTP requests. What if some XML is sent as the value?

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your concern Ali Ben, I will be validating the post requests after I get the script functional, I will try the code from above and report back. Thanks again.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.