通过php中的帖子接收xml文件 [英] receive xml file via post in php

查看:130
本文介绍了通过php中的帖子接收xml文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个可以通过POST接受XML文件然后发送回复的PHP脚本....

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

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

Does anyone have any code that could do this?

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

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?

<?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);






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


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

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

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.

推荐答案

你的方法很好,看起来很漂亮它的正确方法,有一些注释:

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


  • 如果你有PHP5,你可以使用 file_put_contents 作为 file_get_contents 的逆操作,并避免整个 fopen / fwrite / fclose 。但是:

  • 如果您接受的XML POST主体可能很大,那么您的代码现在可能会遇到麻烦。它首先将整个主体加载到内存中,然后将其作为一个大块写出来。对于小帖子来说这很好但是如果文件大小倾向于兆字节,最好用 fopen / fread / fwrite / fclose 来完成它,所以你的内存使用量永远不会超过例如8KB:

  • If you have PHP5, you can use file_put_contents as 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);


  • 当文件的发布频率超过1时,您的文件名生成方法可能会遇到名称冲突第二个(例如,当它们从多个来源发布时)。但我怀疑这只是示例代码,你已经意识到了这一点。

  • 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.

    这篇关于通过php中的帖子接收xml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆