使用 PHP 接收、处理然后返回 XML 数据的最佳方法是什么?
我需要创建一个 PHP 脚本,该脚本通过 HTTP POST 请求接收 XML 输入,对其进行处理,然后返回 XML 响应。我自己花了相当长的时间尝试这个,这就是我到目前为止所得到的。
我首先快速组合了一个 HTML 表单,该表单允许用户以字符串形式提交 XML 数据、以字符串形式提交 XML URI,或者将 XML URI 提交到我的 PHP 脚本。此表单仅用于测试,因为我的任务只是创建此脚本。
在我的 PHP 脚本中,我做了一些输入处理...
// Checks that some XML input has been given
if (!(isset($_POST['xmlinput']))) {
handleErrors("No XML Input Given");
}
// Creates a new DOM Document
$domdoc = new DomDocument;
// Sets the URL of the XML Schema
$xmlschema = "xmlschema.xsd";
// If XML input is a file, tries to load it
if (file_exists($_POST['xmlinput'])) {
if (!$domdoc->load($_POST['xmlinput'])) {
handleErrors("Error in XML File");
}
}
// If XML input is not a file, tries to load it as a string
else {
if (!$domdoc->loadXML($_POST['xmlinput'])) {
handleErrors("Error in XML Document");
}
}
// Validates the XML against the schema
if (!($domdoc->schemaValidate($xmlschema))) {
handleErrors("XML Does Not Conform To Schema");
}
然后我根据 XML 数据做了一些事情,然后我想生成一个 XML 响应。我相当确定我可以在 DOM 或 SimpleXML 中创建 XML,但我根本不明白如何将其返回到原始页面。另外,这是在 PHP 中处理 XML 输入的最佳方法吗?我看过很多关于 php://input 或 $HTTP_RAW_POST_DATA 的帖子,但这些似乎并不比我使用的方法更好。您能给我的任何信息都会有很大的帮助。如果我可以进一步澄清,请告诉我。
I need to create a PHP script that receives XML input via an HTTP POST request, processes it, and then returns an XML response. I have spent quite a while attempting this on my own, and here is what I have so far.
I first quickly threw together an HTML form that allows a user to submit XML data as a string, an XML URI as a string, or an XML URI to my PHP script. This form is for testing only, as I am only tasked with creating this script.
In my PHP script I do some input handling...
// Checks that some XML input has been given
if (!(isset($_POST['xmlinput']))) {
handleErrors("No XML Input Given");
}
// Creates a new DOM Document
$domdoc = new DomDocument;
// Sets the URL of the XML Schema
$xmlschema = "xmlschema.xsd";
// If XML input is a file, tries to load it
if (file_exists($_POST['xmlinput'])) {
if (!$domdoc->load($_POST['xmlinput'])) {
handleErrors("Error in XML File");
}
}
// If XML input is not a file, tries to load it as a string
else {
if (!$domdoc->loadXML($_POST['xmlinput'])) {
handleErrors("Error in XML Document");
}
}
// Validates the XML against the schema
if (!($domdoc->schemaValidate($xmlschema))) {
handleErrors("XML Does Not Conform To Schema");
}
I then do some stuff based on what the XML data is, and then I want to generate an XML response. I am fairly certain I can create XML in DOM or SimpleXML, but I simply do not understand how to return it to the original page. Also, is this the best method to handle XML input in PHP? I have seen a ton of posts about php://input or $HTTP_RAW_POST_DATA but these don't seem to be any better than the method I use. Any information you can give me would be a big help. If I can clarify any more, just let me know.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
非常简单,您使用 SimpleXml 创建 xml 文档,
然后只需
echo $xml->asXML();
如果您需要将 xml 数据发布到 Web 服务,
您可以使用以下代码来实现。
Very simply, you create your xml document using SimpleXml,
then just
echo $xml->asXML();
If you need to post xml data to a webservice,
You can use the following code to acheive that.
您不想使用错误抑制运算符,但添加正确的 选项那:
另一种方法是使用
libxml_use_internal_errors< /代码>
。
响应基本上就是 Web 服务器发送回请求客户端的内容,因此要发送 XML 响应,您只需使用 XML 标头回显您的 XML,例如
You dont want to use the Error Suppression Operator but add the proper Option for that:
An alternative would be to use
libxml_use_internal_errors
.A response is basically just what the webserver sends back to the requesting client, so to send an XML response, you simply echo your XML it with an XML header, e.g.