如何在页面上将cURL返回的数据显示为XML文档?
目前我得到的是这样的:
[something] => 189129
[somethingElse] => exampleContent
[somethingElse1] => someMoreExamples
[somethingElse2] => evenMoreExamples
从这段代码:
$url = "http://example/";
$header[] = 'Accept: application/xml';
$header[] = 'Accept-Encoding: gzip';
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
$xmlResponse = new SimpleXMLElement($response);
?>
<?php
print_r('<pre>');
print_r($xmlResponse);
print_r('</pre>');
?>
有没有办法直接将响应格式化为XML或者使用php后转换它的方法?
感谢。
答案 0 :(得分:1)
修改强>
如果您提取的数据已经是xml,那么您需要做的就是
echo htmlentities($xmlResponse);
以前的答案(后代):
我相信你可以这样做:
// start your xml with a simple doctype
$xml = new SimpleXMLElement('<?xml version="1.0" standalone="yes"?><data/>');
// loop through each array entry, adding the key/value as a child to 'data'
foreach($response AS $key=>$value)
{
$xml->addChild($key, $value);
}
// get the xml
$xmlResponse = $xml->asXML();
// output it, encoding the html characters so it displays ok
echo '<pre>';
echo htmlentities($xmlResponse);
echo '</pre>';