如何在 PHP 中回显 XML 文件

phpserver side programmingprogramming更新于 2025/4/17 15:52:17

借助 PHP 包装器,HTTP URL 可以像本地文件一样运行。可以通过 file_get_contents() 获取 URL 中的内容,并回显。或者使用 readfile 函数读取。

下面是执行相同操作的示例代码 −

$file = file_get_contents('http://example.com/');
echo $file;

下面演示了另一种方法 −

readfile('http://example.com/');
header('Content-type: text/xml'); //显示输出之前必须设置正确的 MIME 类型。

asXML 方法也可以使用。下面是示例代码 −

echo $xml->asXML();
或
$xml->asXML('filename.xml'); //为 xml 文件提供名称。

相关文章