简体   繁体   English

获取响应xml并使用php curl重定向

[英]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: 我在从服务器获取xml响应时遇到了一些麻烦,这是我第一次使用cURL在PHP中使用xml编码(将解析的xml发布到服务器,并接收响应xml)-所有代码如下:

HTML: 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. 这是我得到的输出: 此页面包含以下错误:第8行第8列的错误:文档末尾的额外内容。 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. 从服务器获取响应xml的任何帮助将不胜感激。

cURL handles many details of the HTTP request for you, including headers. cURL为您处理HTTP请求的许多详细信息,包括标头。 Sending the XML document could be simplified like this: 发送XML文档可以这样简化:

[...]
$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. 您正在打印XML文档,稍后再执行标头功能。 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. 附带说明一下,您应该谨慎插入POST参数,而无需在XML中进行验证。 This code: 这段代码:

$title = $_POST['title'];

poses a vulnerability, because an attacker can send anything in HTTP requests. 造成漏洞,因为攻击者可以在HTTP请求中发送任何内容。 What if some XML is sent as the value? 如果发送一些XML作为值怎么办?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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