DOMDocument-直接从加载XML文件php://input.


DOMDocument - Directly load XML file from php://input

目前我有一个PHP文件,它读取发布的XML,然后将其转换/输出为JSON。此文件如下所示:

<?php 
file_put_contents('myxmlfile.xml', file_get_contents('php://input'));
$xmldoc = new DOMDocument();
$xmldoc->load("myxmlfile.xml");
$xpathvar = new DOMXPath($xmldoc);
// Etc etc, for the purpose of my question seeing the rest isn't necessary
// After finishing the conversion I save the file as a JSON file.
file_put_contents('myjsonfile.json', $JSONContent);
?>

我收到的数据是XML格式的。为了转换它,我目前将其保存为一个XML文件,然后在创建一个新的DOMDocument()并加载它之后立即保存。我的问题是,有没有任何方法可以去掉中间人,直接使用file_get_contents()加载XML?

理想情况下是这样的(不起作用):

$xmldoc->load(file_get_contents("php://input"));

如果有人能帮我做这件事,我真的很感激!

感谢

要从字符串而不是文件名加载,请使用loadXML方法。

$xmldoc->loadXML(file_get_contents("php://input"));