Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Usually when I get POST data it's send from a HTML form and the parameters has names, i.e. from <input type="text" name="yourname" />, then I can receive and print this data with php echo $_POST['yourname'];.

Now I am implementing a Java application that should POST data in XML format to a specified URL. I wrote a simple PHP page that I can try to POST data to, but the data is not printed. And since there is no name of any parameters I don't know how I should print it with PHP.

I have tried to send simple XML to the server and the server should respond with MESSAGE: <XML>, but it only response with MESSAGE:. What am I doing wrong?

Here is my PHP-code on the server:

<?php 
echo 'MESSAGE:';
print_r($_POST); ?>

And here is my Java code on the client:

String xmlRequestStatus = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>
    <test></test>";
String contentType = "text/xml";
String charset = "ISO-8859-1";
String request = null;
try {
    request = String.format("%s",
        URLEncoder.encode(xmlRequestStatus, charset));
} catch (UnsupportedEncodingException e1) {
    e1.printStackTrace();
}
URL url = null;
URLConnection connection = null;
OutputStream output = null;
InputStream response = null;
try {
    url = new URL("http://skogsfrisk.se/test/");
} catch (MalformedURLException e) {
    e.printStackTrace();
}

try {
    connection = url.openConnection();
    connection.setDoOutput(true);
    connection.setRequestProperty("Accept-Charset", charset);
    connection.setRequestProperty("Content-Type", contentType);
    output = connection.getOutputStream();
    output.write(request.getBytes("ISO-8859-1"));
    if(output != null) try { output.close(); } catch (IOException e) {}

    response = connection.getInputStream();
    ...
} catch (IOException e) {
    e.printStackTrace();
}
share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

No, $_POST array is being populated only with multipart/form-data enctype.

you can use either fopen() with php://input wrapper or $HTTP_RAW_POST_DATA
http://www.php.net/manual/en/wrappers.php.php

share|improve this answer
    
@Jonas did you try to read the manual page by the link I've posted above? –  Your Common Sense Jun 29 '10 at 18:25
    
Yes, but I did another error, now is it working fine, thanks. –  Jonas Jun 29 '10 at 18:47
    
Thanks, it works fine with this PHP: <?php echo 'MESSAGE:'.$HTTP_RAW_POST_DATA; ?> –  Jonas Jun 29 '10 at 18:49
add comment

Try viewing the page's source in your browser after you post the xml, the xml isn't going to show up if you are viewing the page normally unless you pass it to htmlspecialchars(). print_r($_POST); is xss so be careful.

share|improve this answer
    
print-r will print at least it's own formatting chars like braces –  Your Common Sense Jun 29 '10 at 18:28
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.