receive xml file via post in php

前端 未结 1 849
抹茶落季
抹茶落季 2021-02-01 08:29

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?

So far the only code

相关标签:
1条回答
  • 2021-02-01 09:15

    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_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);
      
    • 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.

    0 讨论(0)
提交回复
热议问题