reading xml post data

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
wackyakmed
Forum Newbie
Posts: 12
Joined: Sat Dec 16, 2006 10:37 am

reading xml post data

Post by wackyakmed »

Hello,

I'm working on a project that submits xml from a client using the xmlhttprequest command in javascript. Then, a php script I've written is supposed to receive the post request, and respond. I'm a newbie when it comes to processing xml requests in php, so I could use some help.

Right now, the client is successfully requesting my php script, and passing the xml data in post format. However, when I try to write the post data to a file in order to verify it's been sent, nothing shows up. only the "Output: " text from the top is displayed in my file.

Here's the php code so far:

Code: Select all

<?php
//write any post requests to a file so I can see what is being returned
$filename = 'xml_log.txt';
$fp = fopen($filename, "a");

$op = "Output: \n";

$xml = simplexml_load_string($_POST);

if (!empty($_POST)) {
    foreach ($_POST as $k => $v) {
        $op = $op . "$k: $v\n";
    }
    exit;
} 

if (!empty($_GET)) {
    foreach ($_GET as $k => $v) {
        $op = $op . "$k: $v\n";
    }
    exit;
} 

$write = fputs($fp, $op);
fclose($fp);

?>
Any help would be greatly appreciated. Thanks!
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

$_POST will always be an array, nothing simplexml_load_string can handle. But you're using it already
foreach ($_POST as $k => $v) {
Does

Code: Select all

<?php
$xml = file_get_contents('php://stdin');
echo htmlentities($xml);
print the xml data?
wackyakmed
Forum Newbie
Posts: 12
Joined: Sat Dec 16, 2006 10:37 am

Post by wackyakmed »

Afraid it doesn't, volka. No output except the "Output: \n".
wackyakmed
Forum Newbie
Posts: 12
Joined: Sat Dec 16, 2006 10:37 am

Post by wackyakmed »

I'm sniffing the packets I'm sending to make sure that they aren't the problem, and here's what the client is sending out to my script(some info changed for anonymity):
POST /xxxxx/xxx.php HTTP/1.1
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US)
Host: http://www.xxxxxxx.com
Content-Length: 139

<?xml version="1.0" encoding="UTF-8"?><request account="username" password="pw"><send param1="test" param2="test send" /></request>
HTTP/1.1 200 OK
Date: Sat, 16 Dec 2006 18:36:33 GMT
Server: Apache/2.0.54
X-Powered-By: PHP/5.1.6
Vary: Accept-Encoding
Transfer-Encoding: chunked
Content-Type: text/html

0
It looks like the data is being sent fine, and I'm just being dense and not reading it right with php. Any ideas?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

oops, sorry. Should have been php://input.
I just tested it with

Code: Select all

<html>
	<head>
		<title>xml request test</title>
		<script type="text/javascript">
			function yadda() {
				req = new XMLHttpRequest();
				req.open("post", "test.php", false);
				req.send(document);
				out = document.getElementById("output");
				out.replaceChild(document.createTextNode(req.responseText), out.firstChild);
			}
		</script>	
	</head>
	<body>
		<button onclick="javascript:yadda()">click</button>
		<div id="output">-</div>
	</body>
</html>
and

Code: Select all

<?php
echo htmlentities(file_get_contents('php://input'));
?>
wackyakmed
Forum Newbie
Posts: 12
Joined: Sat Dec 16, 2006 10:37 am

Post by wackyakmed »

Eh, it's still not working for me. Maybe it's because I'm trying to save it to a variable, and then to a file?

If $op is the string that gets passed to my log file, would this syntax be acceptable?

Code: Select all

$op = $op + htmlentities(file_get_contents('php://input'));
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

string concatenation in php is done via

Code: Select all

$abc . $xyz
see http://de2.php.net/manual/en/language.o ... string.php

What does your html/javascript code look like?
wackyakmed
Forum Newbie
Posts: 12
Joined: Sat Dec 16, 2006 10:37 am

Post by wackyakmed »

lol, I need a separate brain to hold different programming conventions. Here's the relevant JS code:

Code: Select all

function xmlExecute(xml)
{
	var xmlreq = new XMLHttpRequest();
	//post to our website
	xmlreq.open( "POST", "http://www.xxxxx/xxxxx/xml.php", false );
	xmlreq.send( xml );
	print("\nXML POST TEXT: " + xml);
	
        ...
    
	return xmlreq.responseText;
}
Was talking with a developer who set up something similar that we are interfacing with in asp, and he said the data would need to be read as binary data format, not as form data. Would this effect the way I'd need to access the data in my php script?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Yes, it means the data will not show up in $_POST, you need to read the raw POST data.
http://www.php.net/manual/en/wrappers.php.php wrote:php://input allows you to read raw POST data.

Code: Select all

<html>
	<head>
		<title>xml request test</title>
		<script type="text/javascript">
			function xmlExecute(xml)
			{
				var xmlreq = new XMLHttpRequest();
				//post to our website
				xmlreq.open( "POST", "http://localhost/test.php", false );
				xmlreq.send( xml );

				out = document.getElementById("output");
				out.replaceChild(document.createTextNode(xmlreq.responseText), out.firstChild);
				return true;
			} 
		</script>
	</head>
	<body>
		<button onclick="xmlExecute('<data><item>a</item><item>b</item><item>c</item></data>');">click</button>
		<div id="output">-</div>
	</body>
</html>

Code: Select all

<?php
$xml = file_get_contents('php://input');
$doc = simplexml_load_string($xml);
foreach($doc->item as $i) {
	echo $i.'#';
}
?>
Does this work for you? Really try.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

also - don't use htmlentities() when assigning to a variable, it's just there to help you see the output.

Instead, just use:

Code: Select all

$op.=file_get_contents('php://input');
the .= is shorthand for adding on to the end of a string.

Cheers,
Kieran
wackyakmed
Forum Newbie
Posts: 12
Joined: Sat Dec 16, 2006 10:37 am

Post by wackyakmed »

Thanks, Volka and Kieran. I appreciate all the help. Finally got it working, with this code:

Code: Select all

$op .= $doc->asXML();
foreach($doc->request as $i) {
        $op .= $i.'#';
}
Post Reply