简体   繁体   中英

Getting a response xml and redirect with php curl

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.

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM