tags:

views:

5245

answers:

6

Hi,

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

?>

Thanks,

+1  A: 

Your method works, but is needlessly complicated. See the PHP manual for a better way: http://uk.php.net/manual/en/features.file-upload.post-method.php

Piskvor
A: 

Are you talking about uploading an XML file using php? THen check this link from w3schools. The link talks about file upload. You can customise to upload only XML files.

Shoban
A: 

Hi,

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

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.

I'm guessing Ish Kumar's answer is something like what I need - correct?

Thanks....

thegunner
This should be a comment, but I see that you may not have that ability yet.
anonymous coward
+2  A: 

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.

rix0rrr
A: 

Hi Ish,

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);
thegunner
A: 

I have a question.. imagine that i have the next code:

<name>Sousa</name>
<age>20</age>

how can i get that information after the reading ?

regards

rui.pereira