我有以下代码,它显示一个xml页面,如URL'ourcustomersdb.php'。我需要以"我们的客户.xml"结尾的URL显示相同的信息。(希望有道理)
我拥有的代码有效,但我需要最后一行代码来创建 xml 文档:
<?php
header ("Content-Type:text/xml");//Tell browser to expect xml
include ("config/init.php");
$query = "SELECT * FROM ourcustomers";
$result = mysqli_query($mysqli_conn, $query) or die ("Error in query: $query. ".mysql_error());
//Top of xml file
$_xml = '<?xml version="1.0"?>';
$_xml .="<ourcustomers>";
while($row = mysqli_fetch_array($result)) {
$_xml .="<ourcustomer>";
$_xml .="<id>".$row['id']."</id>";
$_xml .="<customer>".$row['customer']."</customer>";
$_xml .="<work_done>".$row['work_done']."</work_done>";
$_xml .="</ourcustomer>";
}
$_xml .="</ourcustomers>";
//Parse and create an xml object using the string
$xmlobj=new SimpleXMLElement($_xml);
//output
//print $xmlobj->asXML();
//write to a file
$xmlobj->asXML(ourcustomers.xml);
?>
似乎不起作用的行是$xmlobj->asXML(ourcustomers.xml)。我需要它来获取从数据库中收集的信息来创建 XML 文件。如果我在最后一个打印注释中留下,它会显示 XML,但在 ourcustomersdb.php 页面上,但我需要将其显示在我们的客户上.xml(希望这是有意义的!有人可以解释为什么,因为这是我第一次尝试这样做,所以我没有完全理解它。
任何帮助都会很棒!
您需要
将ourcustomers.xml
括在引号中,以便它是一个字符串。否则,您将收到语法错误。
$xmlobj->asXML('ourcustomers.xml');
此外,您不必使用 SimpleXMLElement
来编写 XML 文件,您只需使用 file_put_contents
.
file_put_contents('ourcustomers.xml', $_xml);