[英]How to force XML response using PHP CURL
I am testing an XML API which is successfully hitting my account and I am getting a response, but the response appears to be a plain text string with no tags or anything, not even name/pair values. 我正在测试一个成功访问我的帐户的XML API,我得到了一个响应,但响应似乎是一个没有标签或任何内容的纯文本字符串,甚至没有名称/对值。 I need the response to be captured as XML so I can use the XML tags to identify the response variables.
我需要将响应捕获为XML,以便我可以使用XML标记来标识响应变量。
I use a form POST to communicate the data from the form to the CURL page which parses the POST into XML format then uses CURL to send it to the API. 我使用表单POST将表单中的数据传递给CURL页面,该页面将POST解析为XML格式,然后使用CURL将其发送到API。 The API is XML based an I expect an XML response, but instead receive
API是基于XML的,我期望XML响应,而是接收
HELPS! 有所帮助!
CURL REQUEST/RESPONSE PAGE
CURL REQUEST / RESPONSE PAGE
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
$xml = "<?xml version='1.0' encoding='UTF-8'?>";
$xml .= "<request>";
foreach ($_POST as $key => $val) {
$xml .= "<".$key.">". $val ."</".$key.">";
}
$xml .= "<username>user1234</username>";
$xml .= "<password>pass1234</password>";
$xml .= "</request>";
echo "<br/>" . $xml . "<br/><br/>";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.beanstream.com/scripts/process_transaction.aspx');
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/xml'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
$output = curl_exec($ch);
curl_close($ch);
print_r($output);
}
?>
API RESPONSE with header
带标题的 API RESPONSE
HTTP/1.1 200 OK Cache-Control: private Content-Length: 724 Content-Type: application/xml; charset=utf-8 X-Powered-By: ASP.NET X-AspNet-Version: 2.0.50727 Set-Cookie: ASP.NET_SessionId=1p4jp2uoz2de3f45iolold3j; path=/; HttpOnly Date: Wed, 16 Oct 2013 21:39:38 GMT 1100001211ApprovedQCE9HW5ETESTNT53.4810/16/2013 2:39:38 PM1W001Postal/ZIP matches, street address does not match.1VIPCC7
You're just spitting out the result, probably into a browser. 你只是吐出结果,可能是浏览器。 That browser is probably trying to render the XML tags as HTML, and failing because they're not real html.
该浏览器可能正在尝试将XML标记呈现为HTML,并且因为它们不是真正的html而失败。 Unknown tags are simply NOT rendered.
简单地不呈现未知标签。
Try a "view source" and you'll see your xml, or do 尝试“查看源代码”,你会看到你的xml,或者做
echo '<pre>';
echo htmlspecialchars(print_r($output, true));
echo '</pre>';
Alternatively to what Marc B said, you can also forward the Content-Type
in order to make your browser display the XML document the same way it would if you were acessing it directly. 除了Marc B所说的
Content-Type
,您还可以转发Content-Type
,以使浏览器以与直接访问XML文档相同的方式显示XML文档。
You can achieve that by doing this: 你可以这样做:
header('Content-Type: application/xml; charset=utf-8');
echo $output;
I agree with the @Marc B's answer. 我同意@Marc B的回答。 It is what worked for me.
这对我有用。
simplexml_load_string
didnt do what I wished. simplexml_load_string
没有做我想做的事。
I found it tedious to have to loop through each name of the tag and get the XML Object to show all of its contents. 我发现必须遍历标记的每个名称并获取XML对象以显示其所有内容,这很繁琐。
After some more research: 经过一些研究:
echo htmlspecialchars(print_r($response, true));
with htmlspecialchars
you can translate your Curl xml response to XHTML. 使用
htmlspecialchars
您可以将Curl xml响应转换为XHTML。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.