Page 1 of 1
save post xml data to file
Posted: Mon Feb 05, 2007 5:53 am
by ddragas
Hi all
I need to save xml data to file "somefile.xml"
I'm geting xml data from another website (POST) in xml format
Code: Select all
<DeliveryReport>
<message id="56735688" sentdate="" donedate="2005/7/19 22:0:0" status="NOT_SENT" />
</DeliveryReport>
I've tried to put $_POST info file, but it result of writing into file is 'array('
later I've tried
Code: Select all
foreach($_POST as $gt => $vrijednost)
{
$report .= $vrijednost;
}
$file = fopen("$filename","w");
fputs($file,$report);
fclose($file);
but no sucsess. xml file was empty.
please help me
regards ddragas
Posted: Mon Feb 05, 2007 6:03 am
by timvw
You can get the posted xml via: $data = file_get_contents(‘php://input’) ..
Posted: Mon Feb 05, 2007 6:58 am
by ddragas
Thank you for reply
do I need to change 'php://input' with some variable?
or should I write just $_POST because I do not know name of $_POST
Posted: Mon Feb 05, 2007 7:10 am
by m3mn0n
uh, $_POST itself is a superglobal array. So echoing or referencing just $_POST will always return just "array ("
What you need to do is find out what the post variable is called. eg.
$_POST['xml']
Once you have that "xml" or whatever variable name, you can then look to manipulate the data.
I'd recommend running this to see all the variables that are POSTed:
Code: Select all
<?php
echo '<pre>';
print_r ($_POST); // useful & works for any array
echo '</pre>';
?>
Now, if you want to extract the entire XML in its form, then simply work with the POST variable. But if you want just the values and variable names from the XML, such as
$message['id'] = 56735688 then I would recommend using a PHP parser to manipulate/read it.
I'd recommend looking at these links:
-
http://www.php.net/manual/en/ref.xml.php
-
http://www.php.net/manual/en/ref.simplexml.php (highly recommended)
-
http://www.topxml.com/php_simplexml/default.asp
And there is always Google.
Posted: Mon Feb 05, 2007 7:46 am
by ddragas
Thank you for reply but I just can't get it to work
Code: Select all
//***************************************
//SPREMANJE XML FAJLA
//***************************************
$fajl = "../uploadedfiles/xml/report.xml";
$loop = 0;
do
{
if(file_exists($fajl))
{
$loop++;
$fajl = '../uploadedfiles/xml/' . $loop . "_" . "report.xml";
}
}
while
(file_exists($fajl));
$filename = $fajl;
if (!file_exists($filename))
{
touch($filename);
chmod($filename,0666);
}
$data_xml = file_get_contents($_POST['xml']);
$file = fopen("$filename","w");
fputs($file,$data_xml);
fclose($file);
I want to use just $_POST['xml'] and save $_POST['xml'] value into file
Posted: Mon Feb 05, 2007 8:50 am
by timvw
ddragas wrote:Thank you for reply
do I need to change 'php://input' with some variable?
No, you really should use
Code: Select all
$data = file_get_contents('php://input');
If you print_r($_POST) you will see that the data isn't there... print($data) on the other hand...

Posted: Mon Feb 05, 2007 9:22 am
by ddragas
got it working finally.
Thank you all