如何获取xml卷曲响应

时间:2013-09-06 14:44:08

标签: php xml curl

我使用curl方法从服务器获取响应。   但是当ehco这个响应它显示为简单的数据。 但在视图源中它显示xml格式。

任何人都可以告诉我如何将响应打印为xml

我正在使用此代码:

$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
//curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $post_data_tcetra);

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);

1 个答案:

答案 0 :(得分:0)

使用此:

ob_start();
$result = curl_exec($ch);
$xml = ob_get_contents();
ob_end_clean();

echo htmlspecialchars($xml);

您要搜索的实际功能称为htmlspecialchars()。它会将<等符号转换为实体引用&lt;

在我上面的代码中,我们还需要输出缓存( ob _ * )函数,以便我们可以阻止输出直接发送到浏览器。我建议你在下面的代码中使用更好的方法:

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
if ($result === false) {
  // error handling
}
else {
  echo htmlspecialchars($result);
}
相关问题