通过 php 中的 post 接收 xml 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/938811/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 00:23:50  来源:igfitidea点击:

receive xml file via post in php

phpxmlhttppost

提问by

I'm looking for a PHP script that can accept an XML file via a POST, then send a response....

我正在寻找一个 PHP 脚本,它可以通过 POST 接受一个 XML 文件,然后发送一个响应......

Does anyone have any code that could do this?

有没有人有任何代码可以做到这一点?

So far the only code I have is this but not sure about the response or if indeed I am even going in the right direction as XML characters are not saved correctly. Any ideas?

到目前为止,我拥有的唯一代码是这个,但不确定响应,或者我是否确实朝着正确的方向前进,因为 XML 字符没有正确保存。有任何想法吗?

<?php

if ( $_SERVER['REQUEST_METHOD'] === 'POST' ){ 
    $postText = file_get_contents('php://input'); 
}

$datetime=date('ymdHis'); 
$xmlfile = "myfile" . $datetime . ".xml"; 
$FileHandle = fopen($xmlfile, 'w') or die("can't open file"); 
fwrite($FileHandle, $postText); 
fclose($FileHandle);

?>

My files are all empty...the contents is not being written to them. They are being created.

我的文件都是空的……内容没有写入它们。他们正在被创造。

//source html
<form action="quicktest.php" method="post" mimetype="text/xml" enctype="text/xml" name="form1">
<input type="file" name="xmlfile">
<br>

<input type="submit" name="Submit" value="Submit">

</form>

//destination php

$file = $_POST['FILES']['xmlfile'];

$fileContents= file_get_contents($file['tmp_name']);

$datetime=date('ymdHis'); 
$xmlfile="myfile" . $datetime . ".xml"; 
$FileHandle=fopen($xmlfile, 'w') or die("can't open file"); 

fwrite($FileHandle, $postText); 
fclose($FileHandle);


I'm not talking about uploading a file. Someone wants to send an XML file on a regular basis through a HTTP connection.

我不是在谈论上传文件。有人希望通过 HTTP 连接定期发送 XML 文件。

I just need a script running on my server to accept their post to my URL and then save the file to my server and send them a response back saying acknowledged or accepted.

我只需要在我的服务器上运行一个脚本来接受他们发布到我的 URL 的帖子,然后将文件保存到我的服务器并向他们发送回复,说已确认或已接受。

回答by rix0rrr

Your method is fine, and by the looks of it, the proper way to do it, with some notes:

您的方法很好,从外观上看,这是正确的方法,并附有一些注意事项:

  • If you have PHP5, you can use file_put_contentsas the inverse operation of file_get_contents, and avoid the whole fopen/fwrite/fclose. However:
  • If the XML POST bodies you will be accepting may be large, your code right now may run into trouble. It first loads the entire body into memory, then writes it out as one big chunk. That is fine for small posts but if the filesizes tend into megabytes it would be better do to it entirely with fopen/fread/fwrite/fclose, so your memory usage will never exceed for example 8KB:

    $inp = fopen("php://input");
    $outp = fopen("xmlfile" . date("YmdHis") . ".xml", "w");
    
    while (!feof($inp)) {
        $buffer = fread($inp, 8192);
        fwrite($outp, $buffer);
    }
    
    fclose($inp);
    fclose($outp);
    
  • Your filename generation method may run into name collissions when files are posted more regularly than 1 per second (for example when they are posted from multiple sources). But I suspect this is just example code and you are already aware of that.

  • 如果你有 PHP5,你可以file_put_contents作为 的逆操作file_get_contents,避免整个fopen/fwrite/fclose. 然而:
  • 如果您将接受的 XML POST 正文可能很大,那么您现在的代码可能会遇到问题。它首先将整个主体加载到内存中,然后将其作为一个大块写出。这对于小帖子很好,但如果文件大小趋于兆字节,最好完全使用fopen/fread/fwrite/fclose,因此您的内存使用量永远不会超过例如 8KB:

    $inp = fopen("php://input");
    $outp = fopen("xmlfile" . date("YmdHis") . ".xml", "w");
    
    while (!feof($inp)) {
        $buffer = fread($inp, 8192);
        fwrite($outp, $buffer);
    }
    
    fclose($inp);
    fclose($outp);
    
  • 当文件的发布频率超过每秒 1 个时(例如,当它们从多个来源发布时),您的文件名生成方法可能会遇到名称冲突。但我怀疑这只是示例代码,您已经知道了。