Page 1 of 1
reading xml post data
Posted: Sat Dec 16, 2006 10:46 am
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!
Posted: Sat Dec 16, 2006 11:02 am
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?
Posted: Sat Dec 16, 2006 12:12 pm
by wackyakmed
Afraid it doesn't, volka. No output except the "Output: \n".
Posted: Sat Dec 16, 2006 12:41 pm
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?
Posted: Sat Dec 16, 2006 12:59 pm
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'));
?>
Posted: Sat Dec 16, 2006 2:13 pm
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'));
Posted: Sat Dec 16, 2006 2:19 pm
by volka
string concatenation in php is done via
see
http://de2.php.net/manual/en/language.o ... string.php
What does your html/javascript code look like?
Posted: Sat Dec 16, 2006 2:44 pm
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?
Posted: Sat Dec 16, 2006 3:04 pm
by volka
Yes, it means the data will not show up in $_POST, you need to read the 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.
Posted: Sat Dec 16, 2006 3:20 pm
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
Posted: Sat Dec 16, 2006 4:09 pm
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.'#';
}