save post xml data to file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
ddragas
Forum Contributor
Posts: 445
Joined: Sun Apr 18, 2004 4:01 pm

save post xml data to file

Post 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
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

You can get the posted xml via: $data = file_get_contents(‘php://input’) ..
User avatar
ddragas
Forum Contributor
Posts: 445
Joined: Sun Apr 18, 2004 4:01 pm

Post 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
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post 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.
User avatar
ddragas
Forum Contributor
Posts: 445
Joined: Sun Apr 18, 2004 4:01 pm

Post 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
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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... ;)
User avatar
ddragas
Forum Contributor
Posts: 445
Joined: Sun Apr 18, 2004 4:01 pm

Post by ddragas »

got it working finally.

Thank you all
Post Reply